-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware to decode encoding and charset #156
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Decode | ||
====== | ||
|
||
The ``decode`` middleware is used to unapply various content codings. | ||
|
||
:: | ||
|
||
app.use (decode ()); | ||
|
||
app.post ("/", (req, res) => { | ||
var posted_data = req.flatten_utf8 (); | ||
}); | ||
|
||
It is typically put at the top of an application. | ||
|
||
=============== ========================= | ||
Encoding Action | ||
=============== ========================= | ||
deflate `GLib.ZlibDecompressor`_ | ||
gzip and x-gzip `GLib.ZlibDecompressor`_ | ||
identity nothing | ||
=============== ========================= | ||
|
||
.. _GLib.ZlibDecompressor: http://valadoc.org/#!api=gio-2.0/GLib.ZlibDecompressor | ||
|
||
If an encoding is not supported, a ``501 Not Implemented`` is raised and | ||
remaining encodings are *reapplied* on the request. | ||
|
||
To prevent this behavior, the ``DecodeFlags.FORWARD_REMAINING_ENCODINGS`` flag | ||
can be passed to forward unsupported content codings. | ||
|
||
:: | ||
|
||
app.use (decode (DecodeFlags.FORWARD_REMAINING_ENCODINGS)); | ||
|
||
app.use (() => { | ||
if (req.headers.get_one ("Content-Encoding") == "br") { | ||
req.headers.remove ("Content-Encoding"); | ||
req.convert (new BrotliDecompressor ()); | ||
} | ||
return next (); | ||
}); | ||
|
||
app.post ("/", (req, res) => { | ||
var posted_data = req.flatten_utf8 (); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ Middlewares | |
|
||
.. toctree:: | ||
basepath | ||
decode | ||
server-sent-events | ||
subdomain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* This file is part of Valum. | ||
* | ||
* Valum is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* Valum is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Valum. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using GLib; | ||
using VSGI; | ||
|
||
namespace Valum { | ||
|
||
public enum DecodeFlags { | ||
/** | ||
* @since 0.3 | ||
*/ | ||
NONE, | ||
/** | ||
* Forward with the remaining content encodings if they are expected to | ||
* be processed later. | ||
* | ||
* @since 0.3 | ||
*/ | ||
FORWARD_REMAINING_ENCODINGS | ||
} | ||
|
||
/** | ||
* Decode any applied 'Content-Encoding'. | ||
* | ||
* Supports 'gzip', 'deflate' and 'identity', otherwise raise a | ||
* {@link Valum.ServerError.NOT_IMPLEMENTED}. | ||
* | ||
* @since 0.3 | ||
*/ | ||
public HandlerCallback decode (DecodeFlags flags = DecodeFlags.NONE) { | ||
return (req, res, next, ctx) => { | ||
var encodings = Soup.header_parse_list (req.headers.get_list ("Content-Encoding") ?? ""); | ||
|
||
// decode is in the opposite order of application | ||
encodings.reverse (); | ||
|
||
req.headers.remove ("Content-Encoding"); | ||
|
||
for (unowned SList<string> encoding = encodings; encoding != null; encoding = encoding.next) { | ||
switch (encoding.data.down ()) { | ||
case "gzip": | ||
case "x-gzip": | ||
req.headers.set_encoding (Soup.Encoding.EOF); | ||
req.convert (new ZlibDecompressor (ZlibCompressorFormat.GZIP)); | ||
break; | ||
case "deflate": | ||
req.headers.set_encoding (Soup.Encoding.EOF); | ||
req.convert (new ZlibDecompressor (ZlibCompressorFormat.RAW)); | ||
break; | ||
case "identity": | ||
// nothing to do, let's take a break ;) | ||
break; | ||
default: | ||
// reapply remaining encodings | ||
encoding.reverse (); | ||
foreach (var remaining in encoding) { | ||
req.headers.append ("Content-Encoding", remaining); | ||
} | ||
if (DecodeFlags.FORWARD_REMAINING_ENCODINGS in flags) { | ||
return next (); | ||
} else { | ||
throw new ServerError.NOT_IMPLEMENTED ("The '%s' encoding is not supported.", | ||
encoding.data); | ||
} | ||
} | ||
} | ||
|
||
return next (); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* This file is part of Valum. | ||
* | ||
* Valum is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* Valum is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Valum. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using Valum; | ||
using VSGI.Mock; | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_decode_gzip () { | ||
var req = new Request.with_method ("POST", new Soup.URI ("http://127.0.0.1/")); | ||
var res = new Response (req); | ||
|
||
req.headers.append ("Content-Encoding", "gzip"); | ||
|
||
assert ("gzip" == req.headers.get_list ("Content-Encoding")); | ||
|
||
try { | ||
decode () (req, res, () => { | ||
assert (null == req.headers.get_list ("Content-Encoding")); | ||
return true; | ||
}, new Context ()); | ||
} catch (Error err) { | ||
assert_not_reached (); | ||
} | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_decode_xgzip () { | ||
var req = new Request.with_method ("POST", new Soup.URI ("http://127.0.0.1/")); | ||
var res = new Response (req); | ||
|
||
req.headers.append ("Content-Encoding", "x-gzip"); | ||
|
||
assert ("x-gzip" == req.headers.get_list ("Content-Encoding")); | ||
|
||
try { | ||
decode () (req, res, () => { | ||
assert (null == req.headers.get_list ("Content-Encoding")); | ||
return true; | ||
}, new Context ()); | ||
} catch (Error err) { | ||
assert_not_reached (); | ||
} | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_decode_deflate () { | ||
var req = new Request.with_method ("POST", new Soup.URI ("http://127.0.0.1/")); | ||
var res = new Response (req); | ||
|
||
req.headers.append ("Content-Encoding", "deflate"); | ||
|
||
assert ("deflate" == req.headers.get_list ("Content-Encoding")); | ||
|
||
try { | ||
decode () (req, res, () => { | ||
assert (null == req.headers.get_list ("Content-Encoding")); | ||
return true; | ||
}, new Context ()); | ||
} catch (Error err) { | ||
assert_not_reached (); | ||
} | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_decode_identity () { | ||
var req = new Request.with_method ("POST", new Soup.URI ("http://127.0.0.1/")); | ||
var res = new Response (req); | ||
|
||
req.headers.append ("Content-Encoding", "identity"); | ||
|
||
assert ("identity" == req.headers.get_list ("Content-Encoding")); | ||
|
||
try { | ||
decode () (req, res, () => { | ||
assert (null == req.headers.get_list ("Content-Encoding")); | ||
return true; | ||
}, new Context ()); | ||
} catch (Error err) { | ||
assert_not_reached (); | ||
} | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_decode_unknown_encoding () { | ||
var req = new Request.with_method ("POST", new Soup.URI ("http://127.0.0.1/")); | ||
var res = new Response (req); | ||
|
||
req.headers.append ("Content-Encoding", "br, gzip"); | ||
|
||
try { | ||
decode () (req, res, () => { | ||
assert_not_reached (); | ||
}, new Context ()); | ||
assert_not_reached (); | ||
} catch (ServerError.NOT_IMPLEMENTED err) { | ||
assert ("br" == req.headers.get_list ("Content-Encoding")); | ||
} catch (Error err) { | ||
assert_not_reached (); | ||
} | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_decode_forward_remaining_encodings () { | ||
var req = new Request.with_method ("POST", new Soup.URI ("http://127.0.0.1/")); | ||
var res = new Response (req); | ||
|
||
req.headers.append ("Content-Encoding", "gzip, br, deflate"); // brotli is not handled | ||
|
||
try { | ||
decode (DecodeFlags.FORWARD_REMAINING_ENCODINGS) (req, res, () => { | ||
assert ("gzip, br" == req.headers.get_list ("Content-Encoding")); | ||
return true; | ||
}, new Context ()); | ||
} catch (Error err) { | ||
assert_not_reached (); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to peek the gzip header and figure out the size if it's available.