{
u_char *p, *body;
size_t len;
+ ssize_t n;
uint32_t buffer_type;
ngx_buf_t *buf;
njs_int_t ret;
return NJS_DECLINED;
}
- if (r->request_body->temp_file) {
- njs_vm_error(vm, "request body is in a file");
- return NJS_ERROR;
- }
-
cl = r->request_body->bufs;
buf = cl->buf;
+ if (r->request_body->temp_file) {
+ ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
+ "http js reading request body from a temporary file");
+
+ if (buf == NULL || !buf->in_file) {
+ njs_vm_internal_error(vm, "cannot find request body");
+ return NJS_ERROR;
+ }
+
+ len = buf->file_last - buf->file_pos;
+
+ body = ngx_pnalloc(r->pool, len);
+ if (body == NULL) {
+ njs_vm_memory_error(vm);
+ return NJS_ERROR;
+ }
+
+ n = ngx_read_file(buf->file, body, len, buf->file_pos);
+ if (n != (ssize_t) len) {
+ njs_vm_internal_error(vm, "failed to read request body");
+ return NJS_ERROR;
+ }
+
+ goto done;
+ }
+
if (cl->next == NULL) {
len = buf->last - buf->pos;
body = buf->pos;
{
u_char *p, *data;
size_t len;
+ ssize_t n;
JSValue body;
uint32_t buffer_type;
ngx_buf_t *buf;
return JS_UNDEFINED;
}
- if (r->request_body->temp_file) {
- return JS_ThrowTypeError(cx, "request body is in a file");
- }
-
cl = r->request_body->bufs;
buf = cl->buf;
+ if (r->request_body->temp_file) {
+ ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
+ "http js reading request body from a temporary file");
+
+ if (buf == NULL || !buf->in_file) {
+ return JS_ThrowInternalError(cx, "cannot find body file");
+ }
+
+ len = buf->file_last - buf->file_pos;
+
+ data = ngx_pnalloc(r->pool, len);
+ if (data == NULL) {
+ return JS_ThrowOutOfMemory(cx);
+ }
+
+ n = ngx_read_file(buf->file, data, len, buf->file_pos);
+ if (n != (ssize_t) len) {
+ return JS_ThrowInternalError(cx, "failed to read request body");
+ }
+
+ goto done;
+ }
+
if (cl->next == NULL) {
len = buf->last - buf->pos;
data = buf->pos;
js_content test.body;
}
+ location /body_4k {
+ client_body_buffer_size 4k;
+ js_content test.body;
+ }
+
location /in_file {
+ js_content test.body;
+ }
+
+ location /in_file_on {
client_body_in_file_only on;
js_content test.body;
}
+ location /in_file_clean {
+ client_body_in_file_only clean;
+ js_content test.body;
+ }
+
location /read_body_from_temp_file {
client_body_in_file_only clean;
js_content test.read_body_from_temp_file;
EOF
-$t->try_run('no njs request body')->plan(5);
+$t->try_run('no njs request body')->plan(9);
###############################################################################
like(http_post('/body'), qr/REQ-BODY/, 'request body');
-like(http_post('/in_file'), qr/request body is in a file/,
- 'request body in file');
+like(http_post('/in_file'), qr/REQ-BODY/, 'request body in a file');
+like(http_post('/in_file_on'), qr/REQ-BODY/, 'request body in a file on');
+like(http_post('/in_file_clean'), qr/REQ-BODY/, 'request body in a file clean');
like(http_post_big('/body'), qr/200.*^(1234567890){1024}$/ms,
'request body big');
+like(http_post_big('/body_4k'), qr/200.*^(1234567890){1024}$/ms,
+ 'request body big with 4k buffer');
like(http_post_big('/read_body_from_temp_file'),
qr/200.*^(1234567890){1024}$/ms, 'request body big from temp file');
like(http_post('/request_body_cache'),
qr/requestText:string requestBuffer:buffer$/s, 'request body cache');
+$t->stop();
+
+ok(index($t->read_file('error.log'),
+ 'http js reading request body from a temporary file') > 0,
+ 'http request body is in a file warning');
+
###############################################################################
sub http_post {