diff options
author | Nathan Angelacos <nangel@alpinelinux.org> | 2007-11-22 10:51:04 +0000 |
---|---|---|
committer | Nathan Angelacos <nangel@alpinelinux.org> | 2007-11-22 10:51:04 +0000 |
commit | 57e6cb4e5295c30270bbe813317f0f5d4b55dc60 (patch) | |
tree | 7e8d15e6fab731cec7988aab561d605b111d4935 /src | |
parent | 7255680e130274fbb31b61a0ebc916767a773eab (diff) | |
download | haserl-57e6cb4e5295c30270bbe813317f0f5d4b55dc60.tar.bz2 haserl-57e6cb4e5295c30270bbe813317f0f5d4b55dc60.tar.xz |
Optimize changing plusses to spaces in CGIPOST (was checking the strlen each iteration)
Sliding_buffer now reads up to extent size bytes and quits, if extent is given.
This is to read CONTENT_LENGTH bytes and no more.
Diffstat (limited to 'src')
-rw-r--r-- | src/haserl.c | 13 | ||||
-rw-r--r-- | src/sliding_buffer.c | 25 | ||||
-rw-r--r-- | src/sliding_buffer.h | 8 |
3 files changed, 32 insertions, 14 deletions
diff --git a/src/haserl.c b/src/haserl.c index 1f3737c..4419ad4 100644 --- a/src/haserl.c +++ b/src/haserl.c @@ -409,9 +409,9 @@ ReadCGIQueryString (list_t * env) int ReadCGIPOSTValues (list_t * env) { - int content_length = 0; - int max_len; - int i, x; + size_t content_length = 0; + size_t max_len; + size_t i, j, x; sliding_buffer_t sbuf; buffer_t token; unsigned char *data; @@ -432,6 +432,7 @@ ReadCGIPOSTValues (list_t * env) s_buffer_init (&sbuf, 32768); sbuf.fh = STDIN; + sbuf.extent = atol(getenv("CONTENT_LENGTH")); buffer_init (&token); @@ -466,7 +467,8 @@ ReadCGIPOSTValues (list_t * env) } /* change plusses into spaces */ - for (i = 0; i < strlen ((char *) data); i++) + j = strlen((char *) data); + for (i = 0; i <= j; i++) { if (data[i] == '+') { @@ -799,9 +801,6 @@ main (int argc, char *argv[]) free_list_chain (env); free_script_list (scriptchain); - #ifdef DEBUG - closelog(); - #endif return (0); } diff --git a/src/sliding_buffer.c b/src/sliding_buffer.c index 54adea6..177d0d8 100644 --- a/src/sliding_buffer.c +++ b/src/sliding_buffer.c @@ -43,6 +43,8 @@ s_buffer_init (sliding_buffer_t * sbuf, int size) sbuf->len = 0; sbuf->ptr = sbuf->buf; sbuf->bufsize = 0; + sbuf->extent = 0; + sbuf->extent_used = 0; return (sbuf->buf != NULL); /* return true if the alloc succeeded */ } @@ -66,6 +68,7 @@ s_buffer_read (sliding_buffer_t * sbuf, char *matchstr) { int len, pos; int r; + size_t max; /* * if eof and ptr ran off the buffer, then we are done @@ -91,10 +94,24 @@ s_buffer_read (sliding_buffer_t * sbuf, char *matchstr) /* if the filedescriptor is invalid, we are obviously * at an end of file condition. */ - if (fcntl (sbuf->fh, F_GETFL) == -1 ) { - r = 0; } else { - r = read (sbuf->fh, sbuf->buf + len, sbuf->maxsize - len); - } + if (fcntl (sbuf->fh, F_GETFL) == -1) + { + r = 0; + } + else + { + + /* if we have an extent, read the smaller of remaining + * extent or bufsize + */ + max = sbuf->maxsize - len; + if ((sbuf->extent > 0) && (sbuf->maxsize - len) < max) + { + max = sbuf->extent - sbuf->extent_used; + } + r = read (sbuf->fh, sbuf->buf + len, max); + sbuf->extent_used += r; + } /* * only report eof when we've done a read of 0. */ diff --git a/src/sliding_buffer.h b/src/sliding_buffer.h index 838e386..148b369 100644 --- a/src/sliding_buffer.h +++ b/src/sliding_buffer.h @@ -8,10 +8,12 @@ typedef struct { unsigned char *buf; /* pointer to the buffer */ unsigned char *ptr; /* start positon (used internally) */ unsigned char *segment; /* the start position of this segment */ - int len; /* length of this segment */ - int maxsize; /* max size of buffer */ - int bufsize; /* current size of buffer */ + size_t len; /* length of this segment */ + size_t maxsize; /* max size of buffer */ + size_t bufsize; /* current size of buffer */ int eof; /* true if there is no more to read */ + size_t extent; /* if non-zero, max number of bytes to read */ + size_t extent_used; /* how much we have already read */ } sliding_buffer_t; |