Module: H1P
- Defined in:
- lib/h1p/version.rb,
ext/h1p/h1p.c
Defined Under Namespace
Constant Summary collapse
- VERSION =
'1.1'
Class Method Summary collapse
-
.send_body_chunk(io, chunk) ⇒ Object
Sends a body chunk using chunked transfer encoding.
-
.send_chunked_response(io, headers, body = nil) ⇒ Object
Sends an HTTP response with the given headers and body using chunked transfer encoding.
-
.send_response(io, headers, body = nil) ⇒ Object
Sends an HTTP response with the given headers and body.
Class Method Details
.send_body_chunk(io, chunk) ⇒ Object
Sends a body chunk using chunked transfer encoding.
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 |
# File 'ext/h1p/h1p.c', line 1304
VALUE H1P_send_body_chunk(VALUE self, VALUE io, VALUE chunk) {
if (chunk != Qnil) {
if (TYPE(chunk) != T_STRING) chunk = rb_funcall(chunk, ID_to_s, 0);
VALUE len_string = rb_str_new_literal("");
rb_str_modify_expand(len_string, 16);
int len_string_len = sprintf(RSTRING_PTR(len_string), "%lx\r\n", RSTRING_LEN(chunk));
rb_str_set_len(len_string,len_string_len);
VALUE total_written = rb_funcall(io, ID_write, 3, len_string, chunk, STR_CRLF);
RB_GC_GUARD(len_string);
RB_GC_GUARD(chunk);
return total_written;
}
else {
return rb_funcall(io, ID_write, 1, STR_EMPTY_CHUNK);
}
}
|
.send_chunked_response(io, headers, body = nil) ⇒ Object
Sends an HTTP response with the given headers and body using chunked transfer encoding.
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 |
# File 'ext/h1p/h1p.c', line 1329
VALUE H1P_send_chunked_response(VALUE self, VALUE io, VALUE headers) {
VALUE buffer = rb_str_new_literal("");
rb_str_modify_expand(buffer, MAX_RESPONSE_BUFFER_SIZE);
send_response_ctx ctx = {io, buffer, RSTRING_PTR(buffer), 0, 0};
VALUE protocol = rb_hash_aref(headers, STR_pseudo_protocol);
if (protocol == Qnil) protocol = STR_pseudo_protocol_default;
VALUE status = rb_hash_aref(headers, STR_pseudo_status);
if (status == Qnil) status = STR_pseudo_status_default;
send_response_write_status_line(&ctx, protocol, status);
rb_hash_foreach(headers, send_response_write_header, (VALUE)&ctx);
send_response_write_header(STR_transfer_encoding_capitalized, STR_chunked, (VALUE)&ctx);
ctx.buffer_ptr[ctx.buffer_len] = '\r';
ctx.buffer_ptr[ctx.buffer_len + 1] = '\n';
ctx.buffer_len += 2;
send_response_flush_buffer(&ctx);
VALUE len_string = rb_str_new_literal("");
rb_str_modify_expand(len_string, 16);
while (1) {
VALUE chunk = rb_yield(Qnil);
if (chunk == Qnil) {
VALUE written = rb_funcall(io, ID_write, 1, STR_EMPTY_CHUNK);
ctx.total_written += NUM2INT(written);
break;
}
else {
if (TYPE(chunk) != T_STRING) chunk = rb_funcall(chunk, ID_to_s, 0);
int len_string_len = sprintf(RSTRING_PTR(len_string), "%lx\r\n", RSTRING_LEN(chunk));
rb_str_set_len(len_string,len_string_len);
VALUE written = rb_funcall(io, ID_write, 3, len_string, chunk, STR_CRLF);
ctx.total_written += NUM2INT(written);
}
RB_GC_GUARD(chunk);
}
RB_GC_GUARD(len_string);
RB_GC_GUARD(buffer);
return INT2FIX(ctx.total_written);
}
|
.send_response(io, headers, body = nil) ⇒ Object
Sends an HTTP response with the given headers and body.
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 |
# File 'ext/h1p/h1p.c', line 1241
VALUE H1P_send_response(int argc,VALUE *argv, VALUE self) {
if (argc < 2)
rb_raise(eArgumentError, "(wrong number of arguments (expected 2 or more))");
VALUE io = argv[0];
VALUE headers = argv[1];
VALUE body = argc >= 3 ? argv[2] : Qnil;
VALUE buffer = rb_str_new_literal("");
rb_str_modify_expand(buffer, MAX_RESPONSE_BUFFER_SIZE);
send_response_ctx ctx = {io, buffer, RSTRING_PTR(buffer), 0, 0};
char *bodyptr = 0;
unsigned int bodylen = 0;
VALUE protocol = rb_hash_aref(headers, STR_pseudo_protocol);
if (protocol == Qnil) protocol = STR_pseudo_protocol_default;
VALUE status = rb_hash_aref(headers, STR_pseudo_status);
if (status == Qnil) status = STR_pseudo_status_default;
send_response_write_status_line(&ctx, protocol, status);
if (body != Qnil) {
if (TYPE(body) != T_STRING) body = rb_funcall(body, ID_to_s, 0);
bodyptr = RSTRING_PTR(body);
bodylen = RSTRING_LEN(body);
// rb_hash_aset(headers, STR_content_length_capitalized, INT2FIX(bodylen));
}
rb_hash_foreach(headers, send_response_write_header, (VALUE)&ctx);
send_response_write_header(STR_content_length_capitalized, INT2FIX(bodylen), (VALUE)&ctx);
char *endptr = ctx.buffer_ptr + ctx.buffer_len;
endptr[0] = '\r';
endptr[1] = '\n';
ctx.buffer_len += 2;
if (body != Qnil) {
while (bodylen > 0) {
unsigned int chunklen = bodylen;
if (chunklen > MAX_RESPONSE_BUFFER_SIZE) chunklen = MAX_RESPONSE_BUFFER_SIZE;
if (ctx.buffer_len + chunklen > MAX_RESPONSE_BUFFER_SIZE)
send_response_flush_buffer(&ctx);
memcpy(ctx.buffer_ptr + ctx.buffer_len, bodyptr, chunklen);
ctx.buffer_len += chunklen;
bodyptr += chunklen;
bodylen -= chunklen;
}
RB_GC_GUARD(body);
}
send_response_flush_buffer(&ctx);
RB_GC_GUARD(buffer);
return INT2FIX(ctx.total_written);
}
|