@@ -0,0 +1,617 @@
+diff -up nginx-upload-module-2.2/ngx_http_upload_module.c.upload nginx-upload-module-2.2/ngx_http_upload_module.c
+--- nginx-upload-module-2.2/ngx_http_upload_module.c.upload 2012-09-21 11:30:30.000000000 +0700
++++ nginx-upload-module-2.2/ngx_http_upload_module.c 2012-12-17 08:28:50.000000000 +0700
+@@ -267,6 +267,17 @@ typedef struct ngx_http_upload_ctx_s {
+
+ static ngx_int_t ngx_http_upload_test_expect(ngx_http_request_t *r);
+
++static void ngx_http_read_client_request_body_handler(ngx_http_request_t *r);
++static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
++
++static ngx_int_t ngx_http_write_request_body(ngx_http_request_t *r);
++static ngx_int_t ngx_http_request_body_filter(ngx_http_request_t *r, ngx_chain_t *in);
++
++static ngx_int_t ngx_http_request_body_length_filter(ngx_http_request_t *r, ngx_chain_t *in);
++static ngx_int_t ngx_http_request_body_chunked_filter(ngx_http_request_t *r, ngx_chain_t *in);
++
++static ngx_int_t ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in);
++
+ static ngx_int_t ngx_http_upload_handler(ngx_http_request_t *r);
+ static ngx_int_t ngx_http_upload_options_handler(ngx_http_request_t *r);
+ static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r);
+@@ -310,6 +321,7 @@ static ngx_int_t ngx_http_upload_append_
+ static ngx_int_t ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t *range_n);
+ static ngx_int_t ngx_http_upload_parse_range(ngx_str_t *range, ngx_http_upload_range_t *range_n);
+
++
+ static void ngx_http_read_upload_client_request_body_handler(ngx_http_request_t *r);
+ static ngx_int_t ngx_http_do_read_upload_client_request_body(ngx_http_request_t *r);
+ static ngx_int_t ngx_http_process_request_body(ngx_http_request_t *r, ngx_chain_t *body);
+@@ -3084,6 +3096,533 @@ ngx_http_upload_merge_path_value(ngx_con
+ return NGX_CONF_OK;
+ } /* }}} */
+
++static ngx_int_t
++ngx_http_write_request_body(ngx_http_request_t *r)
++{
++ ssize_t n;
++ ngx_chain_t *cl;
++ ngx_temp_file_t *tf;
++ ngx_http_request_body_t *rb;
++ ngx_http_core_loc_conf_t *clcf;
++
++ rb = r->request_body;
++
++ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
++ "http write client request body, bufs %p", rb->bufs);
++
++ if (rb->temp_file == NULL) {
++ tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
++ if (tf == NULL) {
++ return NGX_ERROR;
++ }
++
++ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
++
++ tf->file.fd = NGX_INVALID_FILE;
++ tf->file.log = r->connection->log;
++ tf->path = clcf->client_body_temp_path;
++ tf->pool = r->pool;
++ tf->warn = "a client request body is buffered to a temporary file";
++ tf->log_level = r->request_body_file_log_level;
++ tf->persistent = r->request_body_in_persistent_file;
++ tf->clean = r->request_body_in_clean_file;
++
++ if (r->request_body_file_group_access) {
++ tf->access = 0660;
++ }
++
++ rb->temp_file = tf;
++
++ if (rb->bufs == NULL) {
++ /* empty body with r->request_body_in_file_only */
++
++ if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
++ tf->persistent, tf->clean, tf->access)
++ != NGX_OK)
++ {
++ return NGX_ERROR;
++ }
++
++ return NGX_OK;
++ }
++ }
++
++ if (rb->bufs == NULL) {
++ return NGX_OK;
++ }
++
++ n = ngx_write_chain_to_temp_file(rb->temp_file, rb->bufs);
++
++ /* TODO: n == 0 or not complete and level event */
++
++ if (n == NGX_ERROR) {
++ return NGX_ERROR;
++ }
++
++ rb->temp_file->offset += n;
++
++ /* mark all buffers as written */
++
++ for (cl = rb->bufs; cl; cl = cl->next) {
++ cl->buf->pos = cl->buf->last;
++ }
++
++ rb->bufs = NULL;
++
++ return NGX_OK;
++}
++
++static ngx_int_t
++ngx_http_request_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
++{
++ if (r->headers_in.chunked) {
++ return ngx_http_request_body_chunked_filter(r, in);
++
++ } else {
++ return ngx_http_request_body_length_filter(r, in);
++ }
++}
++
++static ngx_int_t
++ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in)
++{
++#if (NGX_DEBUG)
++ ngx_chain_t *cl;
++#endif
++ ngx_http_request_body_t *rb;
++
++ rb = r->request_body;
++
++#if (NGX_DEBUG)
++
++ for (cl = rb->bufs; cl; cl = cl->next) {
++ ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
++ "http body old buf t:%d f:%d %p, pos %p, size: %z "
++ "file: %O, size: %z",
++ cl->buf->temporary, cl->buf->in_file,
++ cl->buf->start, cl->buf->pos,
++ cl->buf->last - cl->buf->pos,
++ cl->buf->file_pos,
++ cl->buf->file_last - cl->buf->file_pos);
++ }
++
++ for (cl = in; cl; cl = cl->next) {
++ ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
++ "http body new buf t:%d f:%d %p, pos %p, size: %z "
++ "file: %O, size: %z",
++ cl->buf->temporary, cl->buf->in_file,
++ cl->buf->start, cl->buf->pos,
++ cl->buf->last - cl->buf->pos,
++ cl->buf->file_pos,
++ cl->buf->file_last - cl->buf->file_pos);
++ }
++
++#endif
++
++ /* TODO: coalesce neighbouring buffers */
++
++ if (ngx_chain_add_copy(r->pool, &rb->bufs, in) != NGX_OK) {
++ return NGX_HTTP_INTERNAL_SERVER_ERROR;
++ }
++
++ return NGX_OK;
++}
++
++
++static ngx_int_t
++ngx_http_request_body_length_filter(ngx_http_request_t *r, ngx_chain_t *in)
++{
++ size_t size;
++ ngx_int_t rc;
++ ngx_buf_t *b;
++ ngx_chain_t *cl, *tl, *out, **ll;
++ ngx_http_request_body_t *rb;
++
++ rb = r->request_body;
++
++ if (rb->rest == -1) {
++ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
++ "http request body content length filter");
++
++ rb->rest = r->headers_in.content_length_n;
++ }
++
++ out = NULL;
++ ll = &out;
++
++ for (cl = in; cl; cl = cl->next) {
++
++ tl = ngx_chain_get_free_buf(r->pool, &rb->free);
++ if (tl == NULL) {
++ return NGX_HTTP_INTERNAL_SERVER_ERROR;
++ }
++
++ b = tl->buf;
++
++ ngx_memzero(b, sizeof(ngx_buf_t));
++
++ b->temporary = 1;
|