Skip to content

Commit

Permalink
Ruby: Allow empty response headers.
Browse files Browse the repository at this point in the history
If ruby/rack tried to send an empty header, e.g

  app = Proc.new do |env|
      ["200", {
          "Content-Type" => "text/plain",
          "X-Empty-Header" => nil,
      }, ["Hello World\n"]]
  end

  run app

you would get an error like

  2023/10/20 11:24:56 [error] 24829#24829 [unit] nginx#10: Ruby: Wrong header entry 'value' from application
  2023/10/20 11:24:56 [error] 24829#24829 [unit] nginx#10: Ruby: Failed to run ruby script

Empty headers are a thing, so we need to allow the ruby type T_NIL
(along with T_STRING) as a valid header value.

This factors out the setting of value & value_end into a macro as what
these get set to now depends on if we have a T_NIL or a T_STRING and
this prevents an increase in duplicated code.

Signed-off-by: Andrew Clayton <[email protected]>
  • Loading branch information
ac000 committed Nov 8, 2023
1 parent 0b85fe2 commit 5a82c9b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/ruby/nxt_ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,18 @@ nxt_ruby_rack_result_headers(nxt_unit_request_info_t *req, VALUE result,
}


#define NXT_RUBY_SET_HDR_VALUE(r_value, value, value_end) \
do { \
if (TYPE(r_value) == T_STRING) { \
value = RSTRING_PTR(r_value); \
value_end = value + RSTRING_LEN(r_value); \
} else { \
value = ""; \
value_end = value; \
} \
} while (0);


static int
nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg)
{
Expand All @@ -889,16 +901,14 @@ nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg)
goto fail;
}

if (nxt_slow_path(TYPE(r_value) != T_STRING)) {
if (nxt_slow_path(TYPE(r_value) != T_STRING && TYPE(r_value) != T_NIL)) {
nxt_unit_req_error(headers_info->req,
"Ruby: Wrong header entry 'value' from application");

goto fail;
}

value = RSTRING_PTR(r_value);
value_end = value + RSTRING_LEN(r_value);

NXT_RUBY_SET_HDR_VALUE(r_value, value, value_end);
pos = value;

for ( ;; ) {
Expand Down Expand Up @@ -941,11 +951,9 @@ nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg)
headers_info = (void *) (uintptr_t) arg;
rc = &headers_info->rc;

value = RSTRING_PTR(r_value);
value_end = value + RSTRING_LEN(r_value);

key_len = RSTRING_LEN(r_key);

NXT_RUBY_SET_HDR_VALUE(r_value, value, value_end);
pos = value;

for ( ;; ) {
Expand Down

0 comments on commit 5a82c9b

Please sign in to comment.