Skip to content

Commit

Permalink
Added HttpRequest.contentType(Parameters) to avoid wrong handling of …
Browse files Browse the repository at this point in the history
…the "Content-Type" header. Fixes issue #154.
  • Loading branch information
s-ludwig committed Jan 9, 2013
1 parent 68951aa commit 1e5db18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
28 changes: 28 additions & 0 deletions source/vibe/http/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ class HttpRequest {
/// ditto
@property void host(string v) { headers["Host"] = v; }

/** Returns the mime type part of the 'Content-Type' header.
This function gets the pure mime type (e.g. "text/plain")
without any supplimentary parameters such as "charset=...".
Use contentTypeParameters to get any parameter string or
headers["Content-Type"] to get the raw value.
*/
@property string contentType()
const {
auto pv = "Content-Type" in headers;
if( !pv ) return null;
auto idx = std.string.indexOf(*pv, ';');
return idx >= 0 ? (*pv)[0 .. idx] : *pv;
}

/** Returns any supplementary parameters of the 'Content-Type' header.
This is a semicolon separated ist of key/value pairs. Usually, if set,
this contains the character set used for text based content types.
*/
@property string contentTypeParameters()
const {
auto pv = "Content-Type" in headers;
if( !pv ) return null;
auto idx = std.string.indexOf(*pv, ';');
return idx >= 0 ? (*pv)[idx+1 .. $] : null;
}

/** Determines if the connection persists across requests.
*/
@property bool persistent() const
Expand Down
2 changes: 1 addition & 1 deletion source/vibe/http/rest.d
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private HttpServerRequestDelegate jsonMethodHandler(T, string method, alias FUNC
params[i] = fromRestString!P(req.query[param_names[i]]);
} else {
logDebug("%s %s", method, param_names[i]);
enforce(req.headers["Content-Type"] == "application/json", "The Content-Type header needs to be set to application/json.");
enforce(req.contentType == "application/json", "The Content-Type header needs to be set to application/json.");
enforce(req.json.type != Json.Type.Undefined, "The request body does not contain a valid JSON value.");
enforce(req.json.type == Json.Type.Object, "The request body must contain a JSON object with an entry for each parameter.");
enforce(req.json[param_names[i]].type != Json.Type.Undefined, "Missing parameter "~param_names[i]~".");
Expand Down
3 changes: 1 addition & 2 deletions source/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,7 @@ private bool handleRequest(Stream conn, string peer_address, HTTPServerListener
}

if( settings.options & HttpServerOption.ParseJsonBody ){
auto ptype = "Content-Type" in req.headers;
if( ptype && split(*ptype, ";").map!(a => a.strip())().startsWith("application/json") ){
if( req.contentType == "application/json" ){
auto bodyStr = cast(string)req.bodyReader.readAll();
req.json = parseJson(bodyStr);
}
Expand Down

0 comments on commit 1e5db18

Please sign in to comment.