Class: Hyperion::CParser
- Inherits:
-
Object
- Object
- Hyperion::CParser
- Defined in:
- ext/hyperion_http/parser.c
Class Method Summary collapse
-
.build_response_head(rb_status, rb_reason, rb_headers, rb_body_size, rb_keep_alive, rb_date) ⇒ Object
Hyperion::CParser.build_response_head(status, reason, headers, body_size, keep_alive, date_str) -> String.
Instance Method Summary collapse
-
#parse(buffer) ⇒ Object
parse(buffer) -> [Request, end_offset].
Class Method Details
.build_response_head(rb_status, rb_reason, rb_headers, rb_body_size, rb_keep_alive, rb_date) ⇒ Object
Hyperion::CParser.build_response_head(status, reason, headers, body_size,
keep_alive, date_str) -> String
Builds the HTTP/1.1 response head:
"HTTP/1.1 <status> <reason>\r\n"
"<lowercased-key>: <value>\r\n" for each user header (except
content-length / connection — we always set these from the framing
args below, mirroring the rc16 Ruby behaviour where the normalized
hash is overridden in place).
"content-length: <body_size>\r\n"
"connection: <close|keep-alive>\r\n"
"date: <date_str>\r\n" (only if user headers didn't include 'date')
"\r\n"
Header values containing CR/LF raise ArgumentError (response-splitting guard). Bypasses Ruby Hash#each + per-line String#<< allocation; the status line, framing headers, and join slices live in C buffers.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'ext/hyperion_http/parser.c', line 320
static VALUE cbuild_response_head(VALUE self, VALUE rb_status, VALUE rb_reason,
VALUE rb_headers, VALUE rb_body_size,
VALUE rb_keep_alive, VALUE rb_date) {
(void)self;
Check_Type(rb_headers, T_HASH);
Check_Type(rb_reason, T_STRING);
Check_Type(rb_date, T_STRING);
int status = NUM2INT(rb_status);
long body_size = NUM2LONG(rb_body_size);
int keep_alive = RTEST(rb_keep_alive);
/* Most heads fit in 1 KiB; rb_str_cat grows on demand. */
VALUE buf = rb_str_buf_new(1024);
/* Status line: "HTTP/1.1 <status> <reason>\r\n" */
char status_line[48];
int n = snprintf(status_line, sizeof(status_line), "HTTP/1.1 %d ", status);
rb_str_cat(buf, status_line, n);
rb_str_cat(buf, RSTRING_PTR(rb_reason), RSTRING_LEN(rb_reason));
rb_str_cat(buf, "\r\n", 2);
/* Iterate user headers — lowercase key, validate value, skip framing. */
int has_date = 0;
VALUE keys = rb_funcall(rb_headers, rb_intern("keys"), 0);
long n_keys = RARRAY_LEN(keys);
for (long i = 0; i < n_keys; i++) {
VALUE k = rb_ary_entry(keys, i);
VALUE v = rb_hash_aref(rb_headers, k);
VALUE k_s = rb_obj_as_string(k);
VALUE v_s = rb_obj_as_string(v);
VALUE k_lower = rb_funcall(k_s, id_downcase, 0);
const char *k_ptr = RSTRING_PTR(k_lower);
long k_len = RSTRING_LEN(k_lower);
const char *v_ptr = RSTRING_PTR(v_s);
long v_len = RSTRING_LEN(v_s);
/* CRLF injection guard on value. */
for (long j = 0; j < v_len; j++) {
if (v_ptr[j] == '\r' || v_ptr[j] == '\n') {
rb_raise(rb_eArgError, "header %s contains CR/LF",
RSTRING_PTR(rb_inspect(k_lower)));
}
}
/* Drop user-supplied content-length / connection — we always set
* these unconditionally below (matches rc16 Ruby behaviour where
* the normalized hash overwrites in place). */
if (k_len == 14 && memcmp(k_ptr, "content-length", 14) == 0) continue;
if (k_len == 10 && memcmp(k_ptr, "connection", 10) == 0) continue;
if (k_len == 4 && memcmp(k_ptr, "date", 4) == 0) {
has_date = 1;
}
rb_str_cat(buf, k_ptr, k_len);
rb_str_cat(buf, ": ", 2);
rb_str_cat(buf, v_ptr, v_len);
rb_str_cat(buf, "\r\n", 2);
}
/* Framing headers — always emitted. */
char cl_buf[48];
n = snprintf(cl_buf, sizeof(cl_buf), "content-length: %ld\r\n", body_size);
rb_str_cat(buf, cl_buf, n);
if (keep_alive) {
rb_str_cat(buf, "connection: keep-alive\r\n", 24);
} else {
rb_str_cat(buf, "connection: close\r\n", 19);
}
if (!has_date) {
rb_str_cat(buf, "date: ", 6);
rb_str_cat(buf, RSTRING_PTR(rb_date), RSTRING_LEN(rb_date));
rb_str_cat(buf, "\r\n", 2);
}
/* End of head */
rb_str_cat(buf, "\r\n", 2);
return buf;
}
|
Instance Method Details
#parse(buffer) ⇒ Object
parse(buffer) -> [Request, end_offset]
Parse one complete HTTP/1.1 request from ‘buffer`. If buffer doesn’t yet contain a complete request, raise ParseError(“incomplete”). For pipelined input, end_offset is the byte boundary of the first request — Connection carries the rest forward.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'ext/hyperion_http/parser.c', line 239
static VALUE cparser_parse(VALUE self, VALUE buffer) {
Check_Type(buffer, T_STRING);
(void)self;
parser_state_t s;
state_init(&s);
llhttp_t parser;
llhttp_init(&parser, HTTP_REQUEST, &settings);
parser.data = &s;
const char *data = RSTRING_PTR(buffer);
size_t len = (size_t)RSTRING_LEN(buffer);
enum llhttp_errno err = llhttp_execute(&parser, data, len);
/* Custom error flags (set inside callbacks) take precedence. */
if (s.parse_error == 2) {
rb_raise(rb_eUnsupportedError, "%s", s.error_message);
}
if (s.parse_error == 1) {
rb_raise(rb_eParseError, "%s", s.error_message);
}
if (err == HPE_PAUSED_UPGRADE) {
rb_raise(rb_eUnsupportedError, "Upgrade not supported");
}
if (err != HPE_OK && err != HPE_PAUSED) {
const char *reason = llhttp_get_error_reason(&parser);
rb_raise(rb_eParseError, "llhttp: %s",
(reason && *reason) ? reason : llhttp_errno_name(err));
}
if (!s.message_complete) {
rb_raise(rb_eParseError, "incomplete request");
}
/* Compute end_offset. We pause inside on_message_complete, so
* llhttp_get_error_pos returns the byte just after the message
* boundary — exactly the carry-over offset we want. */
size_t consumed;
if (err == HPE_PAUSED) {
const char *epos = llhttp_get_error_pos(&parser);
consumed = epos ? (size_t)(epos - data) : len;
} else {
consumed = len;
}
/* Build the Request. */
VALUE kwargs = rb_hash_new();
rb_hash_aset(kwargs, ID2SYM(id_method_kw), s.method);
rb_hash_aset(kwargs, ID2SYM(id_path_kw), s.path);
rb_hash_aset(kwargs, ID2SYM(id_query_string_kw), s.query_string);
rb_hash_aset(kwargs, ID2SYM(id_http_version_kw), s.http_version);
rb_hash_aset(kwargs, ID2SYM(id_headers_kw), s.headers);
rb_hash_aset(kwargs, ID2SYM(id_body_kw), s.body);
VALUE args[1] = { kwargs };
VALUE request = rb_funcallv_kw(rb_cRequest, id_new, 1, args, RB_PASS_KEYWORDS);
return rb_ary_new_from_args(2, request, ULONG2NUM((unsigned long)consumed));
}
|