httpd-apreq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Schaefer <joe+gm...@sunstarsys.com>
Subject Re: cvs commit: httpd-apreq-2/env/t/cgi-bin .cvsignore
Date Fri, 24 Oct 2003 08:35:22 GMT
Joe Schaefer <joe+apache@sunstarsys.com> writes:

[...]

> The last criticism is that we should eventually get rid of the 
> env_ctx struct in test_cgi.c, and use a static global variable in 
> libapreq_cgi instead.  There's no natural candidate for "env" in a cgi
> context, so IMO we should probably ignore all the "env" arguments and
> let users put anything they like in that slot.

After looking some more at test_cgi.c, there *is* one natural
candidate there for an "env"- the pool!  Here's the (longish) patch
to libapreq_cgi.c and test_cgi.c that implements the above- let me 
know if it's ok to commit (all cgi tests still pass).

Index: env/libapreq_cgi.c
===================================================================
RCS file: /home/cvs/httpd-apreq-2/env/libapreq_cgi.c,v
retrieving revision 1.14
diff -u -r1.14 libapreq_cgi.c
--- env/libapreq_cgi.c	24 Oct 2003 05:25:11 -0000	1.14
+++ env/libapreq_cgi.c	24 Oct 2003 08:30:51 -0000
@@ -64,7 +64,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#define dCTX struct env_ctx *ctx = (struct env_ctx *)env
+#define dP apr_pool_t *p = (apr_pool_t *)env
 
 /**
  * @file libapreq_cgi.c
@@ -82,47 +82,45 @@
  */
 
 struct env_ctx {
-    apr_pool_t         *pool;
     apreq_request_t    *req;
     apreq_jar_t        *jar;
-    apr_bucket_brigade *bb;
-    int                 loglevel;
     apr_status_t        status;
 };
 
+static struct env_ctx ctx;
+
 const char apreq_env_name[] = "CGI";
-const unsigned int apreq_env_magic_number = 20031014;
+const unsigned int apreq_env_magic_number = 20031024;
 
 #define CRLF "\015\012"
 
-#define APREQ_ENV_STATUS(rc_run, k) do { \
-         apr_status_t rc = rc_run; \
-         if (rc != APR_SUCCESS) { \
-             apreq_log(APREQ_DEBUG 0, ctx, \
-                       "Lookup of %s failed: status=%d", k, rc); \
-         } \
+#define APREQ_ENV_STATUS(rc_run, k) do {                                \
+         apr_status_t rc = rc_run;                                      \
+         if (rc != APR_SUCCESS) {                                       \
+             apreq_log(APREQ_DEBUG 0, p,                                \
+                       "Lookup of %s failed: status=%d", k, rc);        \
+         }                                                              \
      } while (0)
 
 APREQ_DECLARE(apr_pool_t *)apreq_env_pool(void *env)
 {
-    dCTX;
-    return ctx->pool;
+    return (apr_pool_t *)env;
 }
 
 APREQ_DECLARE(const char *)apreq_env_query_string(void *env)
 {
-    dCTX;
-    char *value, qs[] = "QUERY_STRING";
-    APREQ_ENV_STATUS(apr_env_get(&value, qs, ctx->pool), qs);
+    dP;
+    char *value = NULL, qs[] = "QUERY_STRING";
+    APREQ_ENV_STATUS(apr_env_get(&value, qs, p), qs);
     return value;
 }
 
 APREQ_DECLARE(const char *)apreq_env_header_in(void *env, 
                                                const char *name)
 {
-    dCTX;
-    char *key = apr_pstrdup(ctx->pool, name);
-    char *k, *value=NULL, *http_key, http[] = "HTTP_";
+    dP;
+    char *key = apr_pstrcat(p, "HTTP_", name, NULL);
+    char *k, *value = NULL;
     for (k = key; *k; ++k) {
         if (*k == '-')
             *k = '_';
@@ -130,67 +128,63 @@
             *k = apr_toupper(*k);
     }
 
-    if (!strcmp(key, "CONTENT_TYPE") || !strcmp(key, "CONTENT_LENGTH")) {
-        APREQ_ENV_STATUS(apr_env_get(&value, key, ctx->pool), key);
-    }
-    else {
-        http_key = (char *) apr_palloc(ctx->pool, sizeof(http) + strlen(key));
-        http_key = strcat(strcpy(http_key, http), key);
-        APREQ_ENV_STATUS(apr_env_get(&value, http_key, ctx->pool), http_key);
+    if (!strcmp(key, "HTTP_CONTENT_TYPE") 
+        || !strcmp(key, "HTTP_CONTENT_LENGTH")) {
+
+        key += 5; /* strlen("HTTP_") */
     }
 
+    APREQ_ENV_STATUS(apr_env_get(&value, key, p), key);
+
     return value;
 }
 
 APREQ_DECLARE(apr_status_t)apreq_env_header_out(void *env, const char *name, 
                                                 char *value)
 {
-    dCTX;
+    dP;
     apr_file_t *out;
     int bytes;
-    apr_file_open_stdout(&out, ctx->pool);
+    apr_file_open_stdout(&out, p);
     bytes = apr_file_printf(out, "%s: %s" CRLF, name, value);
-    apreq_log(APREQ_DEBUG 0, ctx, "Setting header: %s => %s", name, value);
+    apreq_log(APREQ_DEBUG 0, p, "Setting header: %s => %s", name, value);
     return bytes > 0 ? APR_SUCCESS : APR_EGENERAL;
 }
 
 
 APREQ_DECLARE(apreq_jar_t *) apreq_env_jar(void *env, apreq_jar_t *jar)
 {
-    dCTX;
+
     if (jar != NULL) {
-        apreq_jar_t *old_jar = ctx->jar;
-        ctx->jar = jar;
+        apreq_jar_t *old_jar = ctx.jar;
+        ctx.jar = jar;
         return old_jar;
     }
 
-    return ctx->jar;
+    return ctx.jar;
 }
 
 APREQ_DECLARE(apreq_request_t *)apreq_env_request(void *env,
                                                   apreq_request_t *req)
 {
-    dCTX;
 
     if (req != NULL) {
-        apreq_request_t *old_req = ctx->req;
-        ctx->req = req;
+        apreq_request_t *old_req = ctx.req;
+        ctx.req = req;
         return old_req;
     }
-    return ctx->req;
+    return ctx.req;
 }
 
 
 APREQ_DECLARE_LOG(apreq_log)
 {
-    dCTX;
+    dP;
     va_list vp;
-    if (level < ctx->loglevel)
-        return;
 
     va_start(vp, fmt);
     fprintf(stderr, "[%s(%d)] %s\n", file, line, 
-            apr_pvsprintf(ctx->pool,fmt,vp));
+            apr_pvsprintf(p,fmt,vp));
     va_end(vp);
 }
 
@@ -198,20 +192,21 @@
                                            apr_read_type_e block,
                                            apr_off_t bytes)
 {
-    dCTX;
-    if (ctx->bb == NULL) {
-        apr_bucket_alloc_t *alloc = apr_bucket_alloc_create(ctx->pool);
-        apr_file_t *in;
-        apr_bucket *stdin_pipe, *b;
+    dP;
+    apreq_request_t *req = apreq_request(env, NULL);
 
-        ctx->bb = apr_brigade_create(ctx->pool, alloc);
-        apr_file_open_stdin(&in, ctx->pool);
+    if (req->body == NULL) {
+        apr_bucket_alloc_t *alloc = apr_bucket_alloc_create(p);
+        apr_bucket_brigade *bb = apr_brigade_create(p, alloc);
+        apr_bucket *stdin_pipe, *eos = apr_bucket_eos_create(alloc);
+        apr_file_t *in;
+        apr_file_open_stdin(&in, p);
         stdin_pipe = apr_bucket_pipe_create(in,alloc);
-        APR_BRIGADE_INSERT_HEAD(ctx->bb, stdin_pipe);
-        b = apr_bucket_eos_create(alloc);
-        APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
+        APR_BRIGADE_INSERT_HEAD(bb, stdin_pipe);
+        APR_BRIGADE_INSERT_TAIL(bb, eos);
+        ctx.status = apreq_parse_request(req, bb);
     }
-    return apreq_parse_request(apreq_request(env, NULL), ctx->bb);
+    return ctx.status;
 }
 
 /** @} */
Index: env/test_cgi.c
===================================================================
RCS file: /home/cvs/httpd-apreq-2/env/test_cgi.c,v
retrieving revision 1.4
diff -u -r1.4 test_cgi.c
--- env/test_cgi.c	24 Oct 2003 05:25:11 -0000	1.4
+++ env/test_cgi.c	24 Oct 2003 08:30:51 -0000
@@ -61,36 +61,20 @@
 #include "apr_lib.h"
 #include "apr_tables.h"
 
-struct env_ctx {
-    apr_pool_t         *pool;
-    apreq_request_t    *req;
-    apreq_jar_t        *jar;
-    apr_bucket_brigade *bb;
-    int                 loglevel;
-    apr_status_t        status;
-};
-
 static int dump_table(void *count, const char *key, const char *value)
 {
     int *c = (int *) count;
-    int value_len = strlen(value);
-    if(value_len) *c = *c + strlen(key) + value_len;
+    int value_len = apreq_strlen(value);
+    if (value_len) 
+        *c += strlen(key) + value_len;
     return 1;
 }
 
 int main(int argc, char const * const * argv)
 {
-    struct env_ctx *ctx;
     apr_pool_t *pool;
+    apreq_request_t *req;
     const apreq_param_t *foo, *bar, *test, *key;
-    apr_table_t *params;
-    int count = 0;
-    apr_status_t s;
-    const apreq_jar_t *jar;
-    apreq_cookie_t *cookie;
-    apr_ssize_t ssize;
-    apr_size_t size;
-    char *dest;
 
     atexit(apr_terminate);
     if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
@@ -103,61 +87,63 @@
         exit(-1);
     }
 
-    ctx = (struct env_ctx *) apr_pcalloc(pool, sizeof *ctx);
-    ctx->loglevel = 0;
-    ctx->pool = pool;
-    apreq_log(APREQ_DEBUG 0, ctx, "%s", "Creating apreq_request");
-    ctx->req = apreq_request(ctx, NULL);
+    apreq_log(APREQ_DEBUG 0, pool, "%s", "Creating apreq_request");
+    req = apreq_request(pool, NULL);
 
     printf("%s", "Content-Type: text/plain\n\n");
-    apreq_log(APREQ_DEBUG 0, ctx, "%s", "Fetching the parameters");
+    apreq_log(APREQ_DEBUG 0, pool, "%s", "Fetching the parameters");
 
-    foo = apreq_param(ctx->req, "foo");
-    bar = apreq_param(ctx->req, "bar");
+    foo = apreq_param(req, "foo");
+    bar = apreq_param(req, "bar");
 
-    test = apreq_param(ctx->req, "test");
-    key = apreq_param(ctx->req, "key");
+    test = apreq_param(req, "test");
+    key  = apreq_param(req, "key");
 
     if (foo || bar) {
-        if(foo) {
+        if (foo) {
             printf("\t%s => %s\n", "foo", foo->v.data);
-            apreq_log(APREQ_DEBUG 0, ctx, "%s => %s", "foo", foo->v.data);
+            apreq_log(APREQ_DEBUG 0, pool, "%s => %s", "foo", foo->v.data);
         }
-        if(bar) {
+        if (bar) {
             printf("\t%s => %s\n", "bar", bar->v.data);
-            apreq_log(APREQ_DEBUG 0, ctx, "%s => %s", "bar", bar->v.data);
+            apreq_log(APREQ_DEBUG 0, pool, "%s => %s", "bar", bar->v.data);
         }
     }
     
     else if (test && key) {
-        jar = apreq_jar(ctx, NULL);
-        apreq_log(APREQ_DEBUG 0, ctx, "Fetching Cookie %s", key->v.data);
+        const apreq_jar_t *jar = apreq_jar(pool, NULL);
+        apreq_cookie_t *cookie;
+
+        apreq_log(APREQ_DEBUG 0, pool, "Fetching Cookie %s", key->v.data);
         cookie = apreq_cookie(jar, key->v.data);
+
         if (cookie == NULL) {
-            apreq_log(APREQ_DEBUG 0, ctx, 
+            apreq_log(APREQ_DEBUG 0, pool, 
                       "No cookie for %s found!", key->v.data);
             exit(-1);
         }
 
         if (strcmp(test->v.data, "bake") == 0) {
-            s = apreq_cookie_bake(cookie, ctx);
+            apreq_cookie_bake(cookie, pool);
         }
         else if (strcmp(test->v.data, "bake2") == 0) {
-            s = apreq_cookie_bake2(cookie, ctx);
+            apreq_cookie_bake2(cookie, pool);
         }
         else {
-            size = strlen(cookie->v.data);
-            dest = apr_palloc(ctx->pool, size + 1);
-            ssize = apreq_decode(dest, cookie->v.data, size);
-            printf("%s", dest);
+            char *dest = apr_palloc(pool, cookie->v.size + 1);
+            if (apreq_decode(dest, cookie->v.data, cookie->v.size) >= 0)
+                printf("%s", dest);
         }
     }
 
     else { 
-        apreq_log(APREQ_DEBUG 0, ctx, "Fetching all parameters");
-        params = apreq_params(ctx->pool, ctx->req);
+        apr_table_t *params = apreq_params(pool, req);
+        int count = 0;
+
+        apreq_log(APREQ_DEBUG 0, pool, "Fetching all parameters");
+
         if (params == NULL) {
-            apreq_log(APREQ_DEBUG 0, ctx, "No parameters found!");
+            apreq_log(APREQ_DEBUG 0, pool, "No parameters found!");
             exit(-1);
         }
         apr_table_do(dump_table, &count, params, NULL);


Mime
View raw message