#line 1 "kcar.rl" /** * Copyright (c) 2009, 2010 Eric Wong (all bugs are Eric's fault) * Copyright (c) 2005 Zed A. Shaw * You can redistribute it and/or modify it under the same terms as Ruby. */ #include "ruby.h" #include "ext_help.h" #include #include #include #include #include "c_util.h" static VALUE eParserError; static ID id_sq, id_sq_set; /** Defines common length and error messages for input length validation. */ #define DEF_MAX_LENGTH(N, length) \ static const size_t MAX_##N##_LENGTH = length; \ static const char MAX_##N##_LENGTH_ERR[] = \ "HTTP element " # N " is longer than the " # length " allowed length." /** * Validates the max length of given input and throws an ParserError * exception if over. */ #define VALIDATE_MAX_LENGTH(len, N) do { \ if (len > MAX_##N##_LENGTH) \ rb_raise(eParserError, MAX_##N##_LENGTH_ERR); \ } while (0) /* Defines the maximum allowed lengths for various input elements.*/ DEF_MAX_LENGTH(FIELD_NAME, 256); DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024); DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32))); DEF_MAX_LENGTH(REASON, 256); #define UH_FL_CHUNKED 0x1 #define UH_FL_HASBODY 0x2 #define UH_FL_INBODY 0x4 #define UH_FL_HASTRAILER 0x8 #define UH_FL_INTRAILER 0x10 #define UH_FL_INCHUNK 0x20 #define UH_FL_KEEPALIVE 0x40 #define UH_FL_HASHEADER 0x80 struct http_parser { int cs; /* Ragel internal state */ unsigned int flags; size_t mark; size_t offset; union { /* these 2 fields don't nest */ size_t field; size_t query; } start; union { size_t field_len; /* only used during header processing */ size_t dest_offset; /* only used during body processing */ } s; VALUE cont; /* Qfalse: unset, Qnil: ignored header, T_STRING: append */ VALUE status; /* String or Qnil */ union { off_t content; off_t chunk; } len; }; #define REMAINING (unsigned long)(pe - p) #define LEN(AT, FPC) (FPC - buffer - hp->AT) #define MARK(M,FPC) (hp->M = (FPC) - buffer) #define PTR_TO(F) (buffer + hp->F) #define STR_NEW(M,FPC) rb_str_new(PTR_TO(M), LEN(M, FPC)) #define STRIPPED_STR_NEW(M,FPC) stripped_str_new(PTR_TO(M), LEN(M, FPC)) #define HP_FL_TEST(hp,fl) ((hp)->flags & (UH_FL_##fl)) #define HP_FL_SET(hp,fl) ((hp)->flags |= (UH_FL_##fl)) #define HP_FL_UNSET(hp,fl) ((hp)->flags &= ~(UH_FL_##fl)) #define HP_FL_ALL(hp,fl) (HP_FL_TEST(hp, fl) == (UH_FL_##fl)) static int is_lws(char c) { return (c == ' ' || c == '\t'); } static VALUE stripped_str_new(const char *str, long len) { long end; for (end = len - 1; end >= 0 && is_lws(str[end]); end--); return rb_str_new(str, end + 1); } static void finalize_header(struct http_parser *hp) { if ((HP_FL_TEST(hp, HASTRAILER) && ! HP_FL_TEST(hp, CHUNKED))) rb_raise(eParserError, "trailer but not chunked"); } /* * handles values of the "Connection:" header, keepalive is implied * for HTTP/1.1 but needs to be explicitly enabled with HTTP/1.0 * Additionally, we require GET/HEAD requests to support keepalive. */ static void hp_keepalive_connection(struct http_parser *hp, VALUE val) { /* REQUEST_METHOD is always set before any headers */ if (STR_CSTR_CASE_EQ(val, "keep-alive")) { /* basically have HTTP/1.0 masquerade as HTTP/1.1+ */ HP_FL_SET(hp, KEEPALIVE); } else if (STR_CSTR_CASE_EQ(val, "close")) { /* * it doesn't matter what HTTP version or request method we have, * if a server says "Connection: close", we disable keepalive */ HP_FL_UNSET(hp, KEEPALIVE); } else { /* * server could've sent anything, ignore it for now. Maybe * "HP_FL_UNSET(hp, KEEPALIVE);" just in case? * Raising an exception might be too mean... */ } } static void http_version(struct http_parser *hp, VALUE hdr, const char *ptr, size_t len) { if (CONST_MEM_EQ("HTTP/1.1", ptr, len)) { /* HTTP/1.1 implies keepalive unless "Connection: close" is set */ HP_FL_SET(hp, KEEPALIVE); } } static void status_phrase(struct http_parser *hp, VALUE hdr, const char *ptr, size_t len) { long nr; hp->status = rb_str_new(ptr, len); /* RSTRING_PTR is null terminated, ptr is not */ nr = strtol(RSTRING_PTR(hp->status), NULL, 10); if (nr < 100 || nr > 999) rb_raise(eParserError, "invalid status: %s", RSTRING_PTR(hp->status)); if ( !((nr >= 100 && nr <= 199) || nr == 204 || nr == 304) ) HP_FL_SET(hp, HASBODY); } static inline void invalid_if_trailer(struct http_parser *hp) { if (HP_FL_TEST(hp, INTRAILER)) rb_raise(eParserError, "invalid Trailer"); } static void write_cont_value(struct http_parser *hp, char *buffer, const char *p) { char *vptr; long end; long len = LEN(mark, p); long cont_len; if (hp->cont == Qfalse) rb_raise(eParserError, "invalid continuation line"); if (NIL_P(hp->cont)) return; /* we're ignoring this header (probably Status:) */ assert(TYPE(hp->cont) == T_STRING && "continuation line is not a string"); assert(hp->mark > 0 && "impossible continuation line offset"); if (len == 0) return; cont_len = RSTRING_LEN(hp->cont); if (cont_len > 0) { --hp->mark; len = LEN(mark, p); } vptr = PTR_TO(mark); /* normalize tab to space */ if (cont_len > 0) { assert((' ' == *vptr || '\t' == *vptr) && "invalid leading white space"); *vptr = ' '; } for (end = len - 1; end >= 0 && is_lws(vptr[end]); end--); rb_str_buf_cat(hp->cont, vptr, end + 1); } static void write_value(VALUE hdr, struct http_parser *hp, const char *buffer, const char *p) { VALUE f, v; VALUE hclass; const char *fptr = PTR_TO(start.field); size_t flen = hp->s.field_len; const char *vptr; size_t vlen; HP_FL_SET(hp, HASHEADER); /* Rack does not like Status headers, so we never send them */ if (CSTR_CASE_EQ(fptr, flen, "status")) { hp->cont = Qnil; return; } vptr = PTR_TO(mark); vlen = LEN(mark, p); VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE); VALIDATE_MAX_LENGTH(flen, FIELD_NAME); f = rb_str_new(fptr, (long)flen); v = stripped_str_new(vptr, (long)vlen); /* needs more tests for error-checking here */ /* * TODO: * some of these tests might be too strict for real-world HTTP servers, * report real-world examples as we find them: */ if (STR_CSTR_CASE_EQ(f, "connection")) { hp_keepalive_connection(hp, v); } else if (STR_CSTR_CASE_EQ(f, "content-length")) { if (! HP_FL_TEST(hp, HASBODY)) rb_raise(eParserError, "Content-Length with no body expected"); if (HP_FL_TEST(hp, CHUNKED)) rb_raise(eParserError, "Content-Length when chunked Transfer-Encoding is set"); hp->len.content = parse_length(vptr, vlen); if (hp->len.content < 0) rb_raise(eParserError, "invalid Content-Length"); invalid_if_trailer(hp); } else if (STR_CSTR_CASE_EQ(f, "transfer-encoding")) { if (STR_CSTR_CASE_EQ(v, "chunked")) { if (! HP_FL_TEST(hp, HASBODY)) rb_raise(eParserError, "chunked Transfer-Encoding with no body expected"); if (hp->len.content >= 0) rb_raise(eParserError, "chunked Transfer-Encoding when Content-Length is set"); hp->len.chunk = 0; HP_FL_SET(hp, CHUNKED); } invalid_if_trailer(hp); } else if (STR_CSTR_CASE_EQ(f, "trailer")) { if (! HP_FL_TEST(hp, HASBODY)) rb_raise(eParserError, "trailer with no body"); HP_FL_SET(hp, HASTRAILER); invalid_if_trailer(hp); } hclass = CLASS_OF(hdr); if (hclass == rb_cArray) { rb_ary_push(hdr, rb_ary_new3(2, f, v)); hp->cont = v; } else { /* hash-ish, try rb_hash_* first and fall back to slow rb_funcall */ VALUE e; /* try to read the existing value */ if (hclass == rb_cHash) e = rb_hash_aref(hdr, f); else e = rb_funcall(hdr, id_sq, 1, f); if (NIL_P(e)) { /* new value, freeze it since it speeds up MRI slightly */ OBJ_FREEZE(f); if (hclass == rb_cHash) rb_hash_aset(hdr, f, v); else rb_funcall(hdr, id_sq_set, 2, f, v); hp->cont = v; } else { /* * existing value, append to it, Rack 1.x uses newlines to represent * repeated cookies: * { 'Set-Cookie' => "a=b\nc=d" } * becomes: * "Set-Cookie: a=b\r\nSet-Cookie: c=d\r\n" */ rb_str_buf_cat(e, "\n", 1); hp->cont = rb_str_buf_append(e, v); } } } /** Machine **/ #line 363 "kcar.rl" /** Data **/ #line 309 "kcar.c" static const int http_parser_start = 1; static const int http_parser_first_final = 44; static const int http_parser_error = 0; static const int http_parser_en_ChunkedBody = 22; static const int http_parser_en_ChunkedBody_chunk_chunk_end = 27; static const int http_parser_en_Trailers = 36; static const int http_parser_en_main = 1; #line 367 "kcar.rl" static void http_parser_init(struct http_parser *hp) { int cs = 0; memset(hp, 0, sizeof(struct http_parser)); hp->cont = Qfalse; /* zero on MRI, should be optimized away by above */ hp->status = Qnil; hp->len.content = -1; #line 330 "kcar.c" { cs = http_parser_start; } #line 376 "kcar.rl" hp->cs = cs; } /** exec **/ static void http_parser_execute(struct http_parser *hp, VALUE hdr, char *buffer, size_t len) { const char *p, *pe; int cs = hp->cs; size_t off = hp->offset; if (cs == http_parser_first_final) return; assert(off <= len && "offset past end of buffer"); p = buffer+off; pe = buffer+len; assert((void *)(pe - p) == (void *)(len - off) && "pointers aren't same distance"); if (HP_FL_TEST(hp, INCHUNK)) { HP_FL_UNSET(hp, INCHUNK); goto skip_chunk_data_hack; } #line 363 "kcar.c" { if ( p == pe ) goto _test_eof; switch ( cs ) { case 1: if ( (*p) == 72 ) goto tr0; goto st0; st0: cs = 0; goto _out; tr0: #line 303 "kcar.rl" {MARK(mark, p); } goto st2; st2: if ( ++p == pe ) goto _test_eof2; case 2: #line 384 "kcar.c" if ( (*p) == 84 ) goto st3; goto st0; st3: if ( ++p == pe ) goto _test_eof3; case 3: if ( (*p) == 84 ) goto st4; goto st0; st4: if ( ++p == pe ) goto _test_eof4; case 4: if ( (*p) == 80 ) goto st5; goto st0; st5: if ( ++p == pe ) goto _test_eof5; case 5: if ( (*p) == 47 ) goto st6; goto st0; st6: if ( ++p == pe ) goto _test_eof6; case 6: if ( 48 <= (*p) && (*p) <= 57 ) goto st7; goto st0; st7: if ( ++p == pe ) goto _test_eof7; case 7: if ( (*p) == 46 ) goto st8; if ( 48 <= (*p) && (*p) <= 57 ) goto st7; goto st0; st8: if ( ++p == pe ) goto _test_eof8; case 8: if ( 48 <= (*p) && (*p) <= 57 ) goto st9; goto st0; st9: if ( ++p == pe ) goto _test_eof9; case 9: if ( (*p) == 32 ) goto tr9; if ( 48 <= (*p) && (*p) <= 57 ) goto st9; goto st0; tr9: #line 310 "kcar.rl" { http_version(hp, hdr, PTR_TO(mark), LEN(mark, p)); } goto st10; st10: if ( ++p == pe ) goto _test_eof10; case 10: #line 449 "kcar.c" if ( (*p) == 32 ) goto st10; if ( 48 <= (*p) && (*p) <= 57 ) goto tr11; goto st0; tr11: #line 303 "kcar.rl" {MARK(mark, p); } goto st11; st11: if ( ++p == pe ) goto _test_eof11; case 11: #line 463 "kcar.c" switch( (*p) ) { case 10: goto tr12; case 13: goto tr13; case 32: goto st20; } if ( 48 <= (*p) && (*p) <= 57 ) goto st11; goto st0; tr12: #line 311 "kcar.rl" { status_phrase(hp, hdr, PTR_TO(mark), LEN(mark, p)); } goto st12; tr22: #line 307 "kcar.rl" { MARK(mark, p); } #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st12; tr25: #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st12; tr32: #line 307 "kcar.rl" { MARK(mark, p); } #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st12; tr35: #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st12; st12: if ( ++p == pe ) goto _test_eof12; case 12: #line 500 "kcar.c" switch( (*p) ) { case 9: goto st13; case 10: goto tr17; case 13: goto st16; case 32: goto st13; case 33: goto tr19; case 124: goto tr19; case 126: goto tr19; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto tr19; } else if ( (*p) >= 35 ) goto tr19; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto tr19; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto tr19; } else goto tr19; } else goto tr19; goto st0; tr21: #line 307 "kcar.rl" { MARK(mark, p); } goto st13; st13: if ( ++p == pe ) goto _test_eof13; case 13: #line 536 "kcar.c" switch( (*p) ) { case 9: goto tr21; case 10: goto tr22; case 13: goto tr23; case 32: goto tr21; } goto tr20; tr20: #line 307 "kcar.rl" { MARK(mark, p); } goto st14; st14: if ( ++p == pe ) goto _test_eof14; case 14: #line 552 "kcar.c" switch( (*p) ) { case 10: goto tr25; case 13: goto tr26; } goto st14; tr13: #line 311 "kcar.rl" { status_phrase(hp, hdr, PTR_TO(mark), LEN(mark, p)); } goto st15; tr23: #line 307 "kcar.rl" { MARK(mark, p); } #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st15; tr26: #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st15; tr33: #line 307 "kcar.rl" { MARK(mark, p); } #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st15; tr36: #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st15; st15: if ( ++p == pe ) goto _test_eof15; case 15: #line 586 "kcar.c" if ( (*p) == 10 ) goto st12; goto st0; tr17: #line 318 "kcar.rl" { finalize_header(hp); cs = http_parser_first_final; if (HP_FL_TEST(hp, CHUNKED)) cs = http_parser_en_ChunkedBody; /* * go back to Ruby so we can call the Rack application, we'll reenter * the parser iff the body needs to be processed. */ goto post_exec; } goto st44; st44: if ( ++p == pe ) goto _test_eof44; case 44: #line 610 "kcar.c" goto st0; st16: if ( ++p == pe ) goto _test_eof16; case 16: if ( (*p) == 10 ) goto tr17; goto st0; tr19: #line 305 "kcar.rl" { MARK(start.field, p); } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: #line 627 "kcar.c" switch( (*p) ) { case 33: goto st17; case 58: goto tr29; case 124: goto st17; case 126: goto st17; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st17; } else if ( (*p) >= 35 ) goto st17; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st17; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st17; } else goto st17; } else goto st17; goto st0; tr31: #line 307 "kcar.rl" { MARK(mark, p); } goto st18; tr29: #line 306 "kcar.rl" { hp->s.field_len = LEN(start.field, p); } goto st18; st18: if ( ++p == pe ) goto _test_eof18; case 18: #line 664 "kcar.c" switch( (*p) ) { case 9: goto tr31; case 10: goto tr32; case 13: goto tr33; case 32: goto tr31; } goto tr30; tr30: #line 307 "kcar.rl" { MARK(mark, p); } goto st19; st19: if ( ++p == pe ) goto _test_eof19; case 19: #line 680 "kcar.c" switch( (*p) ) { case 10: goto tr35; case 13: goto tr36; } goto st19; st20: if ( ++p == pe ) goto _test_eof20; case 20: if ( (*p) == 10 ) goto st0; goto st21; st21: if ( ++p == pe ) goto _test_eof21; case 21: switch( (*p) ) { case 10: goto tr12; case 13: goto tr13; } goto st21; st22: if ( ++p == pe ) goto _test_eof22; case 22: if ( (*p) == 48 ) goto tr38; if ( (*p) < 65 ) { if ( 49 <= (*p) && (*p) <= 57 ) goto tr39; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto tr39; } else goto tr39; goto st0; tr38: #line 313 "kcar.rl" { hp->len.chunk = step_incr(hp->len.chunk, (*p), 16); if (hp->len.chunk < 0) rb_raise(eParserError, "invalid chunk size"); } goto st23; st23: if ( ++p == pe ) goto _test_eof23; case 23: #line 729 "kcar.c" switch( (*p) ) { case 10: goto tr40; case 13: goto st24; case 48: goto tr38; case 59: goto st33; } if ( (*p) < 65 ) { if ( 49 <= (*p) && (*p) <= 57 ) goto tr39; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto tr39; } else goto tr39; goto st0; tr40: #line 337 "kcar.rl" { HP_FL_SET(hp, INTRAILER); cs = http_parser_en_Trailers; ++p; assert(p <= pe && "buffer overflow after chunked body"); goto post_exec; } goto st45; st45: if ( ++p == pe ) goto _test_eof45; case 45: #line 759 "kcar.c" goto st0; st24: if ( ++p == pe ) goto _test_eof24; case 24: if ( (*p) == 10 ) goto tr40; goto st0; tr39: #line 313 "kcar.rl" { hp->len.chunk = step_incr(hp->len.chunk, (*p), 16); if (hp->len.chunk < 0) rb_raise(eParserError, "invalid chunk size"); } goto st25; st25: if ( ++p == pe ) goto _test_eof25; case 25: #line 780 "kcar.c" switch( (*p) ) { case 10: goto st26; case 13: goto st29; case 59: goto st30; } if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto tr39; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto tr39; } else goto tr39; goto st0; st26: if ( ++p == pe ) goto _test_eof26; case 26: goto tr46; tr46: #line 345 "kcar.rl" { skip_chunk_data_hack: { size_t nr = MIN((size_t)hp->len.chunk, REMAINING); memcpy(RSTRING_PTR(hdr) + hp->s.dest_offset, p, nr); hp->s.dest_offset += nr; hp->len.chunk -= nr; p += nr; assert(hp->len.chunk >= 0 && "negative chunk length"); if ((size_t)hp->len.chunk > REMAINING) { HP_FL_SET(hp, INCHUNK); goto post_exec; } else { p--; {goto st27;} } }} goto st27; st27: if ( ++p == pe ) goto _test_eof27; case 27: #line 823 "kcar.c" switch( (*p) ) { case 10: goto st22; case 13: goto st28; } goto st0; st28: if ( ++p == pe ) goto _test_eof28; case 28: if ( (*p) == 10 ) goto st22; goto st0; st29: if ( ++p == pe ) goto _test_eof29; case 29: if ( (*p) == 10 ) goto st26; goto st0; st30: if ( ++p == pe ) goto _test_eof30; case 30: switch( (*p) ) { case 10: goto st26; case 13: goto st29; case 32: goto st30; case 33: goto st31; case 59: goto st30; case 61: goto st32; case 124: goto st31; case 126: goto st31; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st31; } else if ( (*p) >= 35 ) goto st31; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st31; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st31; } else goto st31; } else goto st31; goto st0; st31: if ( ++p == pe ) goto _test_eof31; case 31: switch( (*p) ) { case 10: goto st26; case 13: goto st29; case 33: goto st31; case 59: goto st30; case 61: goto st32; case 124: goto st31; case 126: goto st31; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st31; } else if ( (*p) >= 35 ) goto st31; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st31; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st31; } else goto st31; } else goto st31; goto st0; st32: if ( ++p == pe ) goto _test_eof32; case 32: switch( (*p) ) { case 10: goto st26; case 13: goto st29; case 33: goto st32; case 59: goto st30; case 124: goto st32; case 126: goto st32; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st32; } else if ( (*p) >= 35 ) goto st32; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st32; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st32; } else goto st32; } else goto st32; goto st0; st33: if ( ++p == pe ) goto _test_eof33; case 33: switch( (*p) ) { case 10: goto tr40; case 13: goto st24; case 32: goto st33; case 33: goto st34; case 59: goto st33; case 61: goto st35; case 124: goto st34; case 126: goto st34; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st34; } else if ( (*p) >= 35 ) goto st34; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st34; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st34; } else goto st34; } else goto st34; goto st0; st34: if ( ++p == pe ) goto _test_eof34; case 34: switch( (*p) ) { case 10: goto tr40; case 13: goto st24; case 33: goto st34; case 59: goto st33; case 61: goto st35; case 124: goto st34; case 126: goto st34; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st34; } else if ( (*p) >= 35 ) goto st34; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st34; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st34; } else goto st34; } else goto st34; goto st0; st35: if ( ++p == pe ) goto _test_eof35; case 35: switch( (*p) ) { case 10: goto tr40; case 13: goto st24; case 33: goto st35; case 59: goto st33; case 124: goto st35; case 126: goto st35; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st35; } else if ( (*p) >= 35 ) goto st35; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st35; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st35; } else goto st35; } else goto st35; goto st0; tr59: #line 307 "kcar.rl" { MARK(mark, p); } #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st36; tr62: #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st36; tr69: #line 307 "kcar.rl" { MARK(mark, p); } #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st36; tr72: #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st36; st36: if ( ++p == pe ) goto _test_eof36; case 36: #line 1053 "kcar.c" switch( (*p) ) { case 9: goto st37; case 10: goto tr54; case 13: goto st40; case 32: goto st37; case 33: goto tr56; case 124: goto tr56; case 126: goto tr56; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto tr56; } else if ( (*p) >= 35 ) goto tr56; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto tr56; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto tr56; } else goto tr56; } else goto tr56; goto st0; tr58: #line 307 "kcar.rl" { MARK(mark, p); } goto st37; st37: if ( ++p == pe ) goto _test_eof37; case 37: #line 1089 "kcar.c" switch( (*p) ) { case 9: goto tr58; case 10: goto tr59; case 13: goto tr60; case 32: goto tr58; } goto tr57; tr57: #line 307 "kcar.rl" { MARK(mark, p); } goto st38; st38: if ( ++p == pe ) goto _test_eof38; case 38: #line 1105 "kcar.c" switch( (*p) ) { case 10: goto tr62; case 13: goto tr63; } goto st38; tr60: #line 307 "kcar.rl" { MARK(mark, p); } #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st39; tr63: #line 309 "kcar.rl" { write_cont_value(hp, buffer, p); } goto st39; tr70: #line 307 "kcar.rl" { MARK(mark, p); } #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st39; tr73: #line 308 "kcar.rl" { write_value(hdr, hp, buffer, p); } goto st39; st39: if ( ++p == pe ) goto _test_eof39; case 39: #line 1135 "kcar.c" if ( (*p) == 10 ) goto st36; goto st0; tr54: #line 332 "kcar.rl" { cs = http_parser_first_final; goto post_exec; } goto st46; st46: if ( ++p == pe ) goto _test_eof46; case 46: #line 1150 "kcar.c" goto st0; st40: if ( ++p == pe ) goto _test_eof40; case 40: if ( (*p) == 10 ) goto tr54; goto st0; tr56: #line 305 "kcar.rl" { MARK(start.field, p); } goto st41; st41: if ( ++p == pe ) goto _test_eof41; case 41: #line 1167 "kcar.c" switch( (*p) ) { case 33: goto st41; case 58: goto tr66; case 124: goto st41; case 126: goto st41; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) goto st41; } else if ( (*p) >= 35 ) goto st41; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st41; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) goto st41; } else goto st41; } else goto st41; goto st0; tr68: #line 307 "kcar.rl" { MARK(mark, p); } goto st42; tr66: #line 306 "kcar.rl" { hp->s.field_len = LEN(start.field, p); } goto st42; st42: if ( ++p == pe ) goto _test_eof42; case 42: #line 1204 "kcar.c" switch( (*p) ) { case 9: goto tr68; case 10: goto tr69; case 13: goto tr70; case 32: goto tr68; } goto tr67; tr67: #line 307 "kcar.rl" { MARK(mark, p); } goto st43; st43: if ( ++p == pe ) goto _test_eof43; case 43: #line 1220 "kcar.c" switch( (*p) ) { case 10: goto tr72; case 13: goto tr73; } goto st43; } _test_eof2: cs = 2; goto _test_eof; _test_eof3: cs = 3; goto _test_eof; _test_eof4: cs = 4; goto _test_eof; _test_eof5: cs = 5; goto _test_eof; _test_eof6: cs = 6; goto _test_eof; _test_eof7: cs = 7; goto _test_eof; _test_eof8: cs = 8; goto _test_eof; _test_eof9: cs = 9; goto _test_eof; _test_eof10: cs = 10; goto _test_eof; _test_eof11: cs = 11; goto _test_eof; _test_eof12: cs = 12; goto _test_eof; _test_eof13: cs = 13; goto _test_eof; _test_eof14: cs = 14; goto _test_eof; _test_eof15: cs = 15; goto _test_eof; _test_eof44: cs = 44; goto _test_eof; _test_eof16: cs = 16; goto _test_eof; _test_eof17: cs = 17; goto _test_eof; _test_eof18: cs = 18; goto _test_eof; _test_eof19: cs = 19; goto _test_eof; _test_eof20: cs = 20; goto _test_eof; _test_eof21: cs = 21; goto _test_eof; _test_eof22: cs = 22; goto _test_eof; _test_eof23: cs = 23; goto _test_eof; _test_eof45: cs = 45; goto _test_eof; _test_eof24: cs = 24; goto _test_eof; _test_eof25: cs = 25; goto _test_eof; _test_eof26: cs = 26; goto _test_eof; _test_eof27: cs = 27; goto _test_eof; _test_eof28: cs = 28; goto _test_eof; _test_eof29: cs = 29; goto _test_eof; _test_eof30: cs = 30; goto _test_eof; _test_eof31: cs = 31; goto _test_eof; _test_eof32: cs = 32; goto _test_eof; _test_eof33: cs = 33; goto _test_eof; _test_eof34: cs = 34; goto _test_eof; _test_eof35: cs = 35; goto _test_eof; _test_eof36: cs = 36; goto _test_eof; _test_eof37: cs = 37; goto _test_eof; _test_eof38: cs = 38; goto _test_eof; _test_eof39: cs = 39; goto _test_eof; _test_eof46: cs = 46; goto _test_eof; _test_eof40: cs = 40; goto _test_eof; _test_eof41: cs = 41; goto _test_eof; _test_eof42: cs = 42; goto _test_eof; _test_eof43: cs = 43; goto _test_eof; _test_eof: {} _out: {} } #line 403 "kcar.rl" post_exec: /* "_out:" also goes here */ if (hp->cs != http_parser_error) hp->cs = cs; hp->offset = p - buffer; assert(p <= pe && "buffer overflow after parsing execute"); assert(hp->offset <= len && "offset longer than length"); } static struct http_parser *data_get(VALUE self) { struct http_parser *hp; Data_Get_Struct(self, struct http_parser, hp); assert(hp && "failed to extract http_parser struct"); return hp; } static void mark(void *ptr) { struct http_parser *hp = ptr; rb_gc_mark(hp->cont); rb_gc_mark(hp->status); } static VALUE alloc(VALUE klass) { struct http_parser *hp; return Data_Make_Struct(klass, struct http_parser, mark, -1, hp); } /** * call-seq: * Kcar::Parser.new => parser * * Creates a new parser. * * Document-method: reset * * call-seq: * parser.reset => parser * * Resets the parser so it can be reused by another client */ static VALUE initialize(VALUE self) { http_parser_init(data_get(self)); return self; } static void advance_str(VALUE str, off_t nr) { long len = RSTRING_LEN(str); if (len == 0) return; rb_str_modify(str); assert(nr <= len && "trying to advance past end of buffer"); len -= nr; if (len > 0) /* unlikely, len is usually 0 */ memmove(RSTRING_PTR(str), RSTRING_PTR(str) + nr, len); rb_str_set_len(str, len); } /** * call-seq: * parser.body_bytes_left => nil or Integer * * Returns the number of bytes left to run through Parser#filter_body. * This will initially be the value of the "Content-Length" HTTP header * after header parsing is complete and will decrease in value as * Parser#filter_body is called for each chunk. This should return * zero for responses with no body. * * This will return nil on "Transfer-Encoding: chunked" responses as * well as HTTP/1.0 responses where Content-Length is not set */ static VALUE body_bytes_left(VALUE self) { struct http_parser *hp = data_get(self); if (HP_FL_TEST(hp, CHUNKED)) return Qnil; if (hp->len.content >= 0) return OFFT2NUM(hp->len.content); return Qnil; } /** * call-seq: * parser.body_bytes_left = Integer * * Sets the number of bytes left to download for HTTP responses * with "Content-Length". This raises RuntimeError for chunked * responses. */ static VALUE body_bytes_left_set(VALUE self, VALUE bytes) { struct http_parser *hp = data_get(self); if (HP_FL_TEST(hp, CHUNKED)) rb_raise(rb_eRuntimeError, "body_bytes_left= is not for chunked bodies"); hp->len.content = NUM2OFFT(bytes); return bytes; } /** * Document-method: chunked * call-seq: * parser.chunked? => true or false * * This is used to detect if a response uses chunked Transfer-Encoding or not. */ static VALUE chunked(VALUE self) { struct http_parser *hp = data_get(self); return HP_FL_TEST(hp, CHUNKED) ? Qtrue : Qfalse; } /** * Document-method: headers * call-seq: * parser.headers(hdr, data) => hdr or nil * * Takes a Hash and a String of data, parses the String of data filling * in the Hash returning the Hash if parsing is finished, nil otherwise * When returning the hdr Hash, it may modify data to point to where * body processing should begin. * * Raises ParserError if there are parsing errors. */ static VALUE headers(VALUE self, VALUE hdr, VALUE data) { struct http_parser *hp = data_get(self); http_parser_execute(hp, hdr, RSTRING_PTR(data), RSTRING_LEN(data)); VALIDATE_MAX_LENGTH(hp->offset, HEADER); if (hp->cs == http_parser_first_final || hp->cs == http_parser_en_ChunkedBody) { advance_str(data, hp->offset + 1); hp->offset = 0; if (HP_FL_TEST(hp, INTRAILER)) return hdr; else return rb_ary_new3(2, hp->status, hdr); } if (hp->cs == http_parser_error) rb_raise(eParserError, "Invalid HTTP format, parsing fails."); return Qnil; } static int chunked_eof(struct http_parser *hp) { return ((hp->cs == http_parser_first_final) || HP_FL_TEST(hp, INTRAILER)); } /** * call-seq: * parser.body_eof? => true or false * * Detects if we're done filtering the body or not. This can be used * to detect when to stop calling Parser#filter_body. */ static VALUE body_eof(VALUE self) { struct http_parser *hp = data_get(self); if (!HP_FL_TEST(hp, HASHEADER) && HP_FL_ALL(hp, KEEPALIVE)) return Qtrue; if (HP_FL_TEST(hp, CHUNKED)) return chunked_eof(hp) ? Qtrue : Qfalse; if (! HP_FL_TEST(hp, HASBODY)) return Qtrue; return hp->len.content == 0 ? Qtrue : Qfalse; } /** * call-seq: * parser.keepalive? => true or false * * This should be used to detect if a request can really handle * keepalives and pipelining. Currently, the rules are: * * 1. MUST be HTTP/1.1 +or+ HTTP/1.0 with "Connection: keep-alive" * 2. MUST NOT have "Connection: close" set * 3. If there is a response body, either a) Content-Length is set * or b) chunked encoding is used */ static VALUE keepalive(VALUE self) { struct http_parser *hp = data_get(self); if (HP_FL_ALL(hp, KEEPALIVE)) { if (HP_FL_TEST(hp, HASHEADER) && HP_FL_TEST(hp, HASBODY) ) { if (HP_FL_TEST(hp, CHUNKED) || (hp->len.content >= 0)) return Qtrue; /* unknown Content-Length and not chunked, we must assume close */ return Qfalse; } else { /* 100 Continue, 304 Not Modified, etc... */ return Qtrue; } } return Qfalse; } /** * call-seq: * parser.filter_body(buf, data) => nil/data * * Takes a String of +data+, will modify data if dechunking is done. * Returns +nil+ if there is more data left to process. Returns * +data+ if body processing is complete. When returning +data+, * it may modify +data+ so the start of the string points to where * the body ended so that trailer processing can begin. * * Raises ParserError if there are dechunking errors. * Basically this is a glorified memcpy(3) that copies +data+ * into +buf+ while filtering it through the dechunker. */ static VALUE filter_body(VALUE self, VALUE buf, VALUE data) { struct http_parser *hp = data_get(self); char *dptr; long dlen; dptr = RSTRING_PTR(data); dlen = RSTRING_LEN(data); StringValue(buf); rb_str_modify(buf); rb_str_resize(buf, dlen); /* we can never copy more than dlen bytes */ OBJ_TAINT(buf); /* keep weirdo $SAFE users happy */ if (!HP_FL_TEST(hp, CHUNKED)) rb_raise(rb_eRuntimeError, "filter_body is only for chunked bodies"); if (!chunked_eof(hp)) { hp->s.dest_offset = 0; http_parser_execute(hp, buf, dptr, dlen); if (hp->cs == http_parser_error) rb_raise(eParserError, "Invalid HTTP format, parsing fails."); assert(hp->s.dest_offset <= hp->offset && "destination buffer overflow"); advance_str(data, hp->offset); rb_str_set_len(buf, hp->s.dest_offset); if (RSTRING_LEN(buf) == 0 && chunked_eof(hp)) { assert(hp->len.chunk == 0 && "chunk at EOF but more to parse"); } else { data = Qnil; } } hp->offset = 0; /* for trailer parsing */ return data; } void Init_kcar_ext(void) { VALUE mKcar = rb_define_module("Kcar"); VALUE cParser = rb_define_class_under(mKcar, "Parser", rb_cObject); eParserError = rb_define_class_under(mKcar, "ParserError", rb_eIOError); rb_define_alloc_func(cParser, alloc); rb_define_method(cParser, "initialize", initialize, 0); rb_define_method(cParser, "reset", initialize, 0); rb_define_method(cParser, "headers", headers, 2); rb_define_method(cParser, "trailers", headers, 2); rb_define_method(cParser, "filter_body", filter_body, 2); rb_define_method(cParser, "body_bytes_left", body_bytes_left, 0); rb_define_method(cParser, "body_bytes_left=", body_bytes_left_set, 1); rb_define_method(cParser, "body_eof?", body_eof, 0); rb_define_method(cParser, "keepalive?", keepalive, 0); rb_define_method(cParser, "chunked?", chunked, 0); /* * The maximum size a single chunk when using chunked transfer encoding. * This is only a theoretical maximum used to detect errors in clients, * it is highly unlikely to encounter clients that send more than * several kilobytes at once. */ rb_define_const(cParser, "CHUNK_MAX", OFFT2NUM(UH_OFF_T_MAX)); /* * The maximum size of the body as specified by Content-Length. * This is only a theoretical maximum, the actual limit is subject * to the limits of the file system used for +Dir.tmpdir+. */ rb_define_const(cParser, "LENGTH_MAX", OFFT2NUM(UH_OFF_T_MAX)); id_sq = rb_intern("[]"); id_sq_set = rb_intern("[]="); }