first complete header/post management v0.3.0
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Mon, 2 Jul 2012 05:27:09 +0000 (07:27 +0200)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Mon, 2 Jul 2012 05:27:09 +0000 (07:27 +0200)
mod_proxy_fdpass.c

index 691ca08..0e2ba33 100644 (file)
@@ -270,6 +270,63 @@ static int headers_builder(void *rec, const char *key, const char *value)
     sprintf(s, "%s%s:%s\n", s, key, value);
 }
 
+#define CTRL_BUFF_MAX_SZ (8*1024)
+
+#define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
+
+int util_read(request_rec *r, const char **rbuf)
+{
+    int rc;
+
+    if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
+        return rc;
+    }
+
+    if (ap_should_client_block(r)) {
+        char argsbuffer[HUGE_STRING_LEN];
+        int rsize, len_read, rpos=0;
+        long length = r->remaining;
+        *rbuf = (char *)apr_pcalloc(r->pool, length +1);
+
+        while ((len_read = ap_get_client_block(r, argsbuffer,
+sizeof(argsbuffer))) > 0) {
+            if ((rpos + len_read) > length) {
+                rsize = length - rpos;
+            } else {
+                rsize = len_read;
+            }
+
+            memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
+            rpos += rsize;
+        }
+
+    }
+
+    return rc;
+}
+
+int read_post(request_rec *r, const char **data)
+{
+    const char *type;
+    int rc = OK;
+
+    if (r->method_number != M_POST) {
+        return rc;
+    }
+
+    type = apr_table_get(r->headers_in, "Content-Type");
+    if (strcasecmp(type, DEFAULT_ENCTYPE) != 0) {
+        return DECLINED;
+    }
+
+    if ((rc = util_read(r, data)) != OK) {
+        return rc;
+    }
+
+    return OK;
+}
+
+
 // TODO: sanitize calloc
 static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
                               proxy_server_conf *conf,
@@ -284,11 +341,13 @@ static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
     int ctrlrawsock[2];
     apr_socket_t *ctrlsock = NULL, *clientctrlsock = NULL;
     apr_size_t wrlen;
+    const char *post_data = NULL;
 
-    if ((headers_out = calloc(8*1024, 1)) != NULL) {
+    if ((headers_out = calloc(CTRL_BUFF_MAX_SZ, 1)) != NULL) {
+        sprintf(headers_out, "The-Request:%s\n", r->the_request);
         apr_table_do(headers_builder, headers_out, r->headers_in, NULL);
     }
-
+    read_post(r, &post_data);
 
     {
         int mop_fd;
@@ -301,10 +360,6 @@ static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
         close(mop_fd);
 
     }
-    /*
-    if (headers_out)
-        free(headers_out);
-    */
     if (strncasecmp(url, "fd://", 5) == 0) {
         url += 5;
     }
@@ -376,6 +431,7 @@ static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
                       "proxy: FD: send_socket failed:");
         return HTTP_INTERNAL_SERVER_ERROR;
     }
+    strcat(headers_out, "\n");
     wrlen = strlen(headers_out);
     rv = apr_socket_send(ctrlsock, headers_out, &wrlen);
     if (rv != APR_SUCCESS) {
@@ -383,7 +439,19 @@ static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
                       "proxy: FD: send headers failed");
         return HTTP_INTERNAL_SERVER_ERROR;
     }
+    if (post_data) {
+        wrlen = strlen(post_data);
+        rv = apr_socket_send(ctrlsock, post_data, &wrlen);
+        if (rv != APR_SUCCESS) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
+                          "proxy: FD: send post failed");
+            return HTTP_INTERNAL_SERVER_ERROR;
+        }
+    }
     apr_socket_shutdown(ctrlsock, APR_SHUTDOWN_READWRITE);
+    if (headers_out)
+        free(headers_out);
+
     {
         apr_socket_t *dummy;
         /* Create a dummy unconnected socket, and set it as the one we were