You are viewing a plain text version of this content. The canonical link for it is here.
Posted to apreq-cvs@httpd.apache.org by jo...@apache.org on 2003/10/25 00:09:52 UTC
cvs commit: httpd-apreq-2/src apreq_parsers.c
joes 2003/10/24 15:09:52
Modified: env libapreq_cgi.c test_cgi.c
src apreq_parsers.c
Log:
Make libapreq_cgi use a pool as the underlying env
Revision Changes Path
1.15 +47 -54 httpd-apreq-2/env/libapreq_cgi.c
Index: libapreq_cgi.c
===================================================================
RCS file: /home/cvs/httpd-apreq-2/env/libapreq_cgi.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- libapreq_cgi.c 24 Oct 2003 05:25:11 -0000 1.14
+++ libapreq_cgi.c 24 Oct 2003 22:09:52 -0000 1.15
@@ -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
@@ -81,48 +81,44 @@
* @{
*/
-struct env_ctx {
- apr_pool_t *pool;
+static struct {
apreq_request_t *req;
apreq_jar_t *jar;
- apr_bucket_brigade *bb;
- int loglevel;
apr_status_t status;
-};
+} 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 +126,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 +190,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;
}
/** @} */
1.5 +32 -46 httpd-apreq-2/env/test_cgi.c
Index: test_cgi.c
===================================================================
RCS file: /home/cvs/httpd-apreq-2/env/test_cgi.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- test_cgi.c 24 Oct 2003 05:25:11 -0000 1.4
+++ test_cgi.c 24 Oct 2003 22:09:52 -0000 1.5
@@ -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_pcalloc(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);
1.37 +1 -1 httpd-apreq-2/src/apreq_parsers.c
Index: apreq_parsers.c
===================================================================
RCS file: /home/cvs/httpd-apreq-2/src/apreq_parsers.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- apreq_parsers.c 14 Oct 2003 18:15:38 -0000 1.36
+++ apreq_parsers.c 24 Oct 2003 22:09:52 -0000 1.37
@@ -438,7 +438,7 @@
apr_bucket *e;
struct hdr_ctx *ctx;
-/* use parser->v.status to maintain state */
+/* use ctx->status to maintain state */
#define HDR_NAME 0
#define HDR_GAP 1
#define HDR_VALUE 2