Skip to content
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

puma/http_parser: support duplicate headers #818

Merged
merged 2 commits into from Nov 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions ext/puma_http11/org/jruby/puma/Http11.java
Expand Up @@ -82,11 +82,11 @@ public void validateMaxLength(int len, int max, String msg) {
private Http11Parser.FieldCB http_field = new Http11Parser.FieldCB() {
public void call(Object data, int field, int flen, int value, int vlen) {
RubyHash req = (RubyHash)data;
RubyString v,f;
RubyString f;
IRubyObject v;
validateMaxLength(flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
validateMaxLength(vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);

v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen));
ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen);
for(int i = 0,j = b.length();i<j;i++) {
if((b.get(i) & 0xFF) == '-') {
Expand All @@ -104,7 +104,16 @@ public void call(Object data, int field, int flen, int value, int vlen) {
f = RubyString.newString(runtime, "HTTP_");
f.cat(b);
}
req.op_aset(req.getRuntime().getCurrentContext(), f,v);

b = new ByteList(Http11.this.hp.parser.buffer, value, vlen);
v = req.op_aref(req.getRuntime().getCurrentContext(), f);
if (v.isNil()) {
req.op_aset(req.getRuntime().getCurrentContext(), f, RubyString.newString(runtime, b));
} else {
RubyString vs = v.convertToString();
vs.cat(", ");
vs.cat(b);
}
}
};

Expand Down
16 changes: 12 additions & 4 deletions ext/puma_http11/puma_http11.c
Expand Up @@ -176,14 +176,12 @@ static VALUE find_common_field_value(const char *field, size_t flen)
void http_field(puma_parser* hp, const char *field, size_t flen,
const char *value, size_t vlen)
{
VALUE v = Qnil;
VALUE f = Qnil;
VALUE v;

VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);

v = rb_str_new(value, vlen);

f = find_common_field_value(field, flen);

if (f == Qnil) {
Expand All @@ -201,7 +199,17 @@ void http_field(puma_parser* hp, const char *field, size_t flen,
f = rb_str_new(hp->buf, new_size);
}

rb_hash_aset(hp->request, f, v);
/* check for duplicate header */
v = rb_hash_aref(hp->request, f);

if (v == Qnil) {
v = rb_str_new(value, vlen);
rb_hash_aset(hp->request, f, v);
} else {
/* if duplicate header, normalize to comma-separated values */
rb_str_cat2(v, ", ");
rb_str_cat(v, value, vlen);
}
}

void request_method(puma_parser* hp, const char *at, size_t length)
Expand Down