1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/* Copyright 2000-2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* Modified by Shugo Maeda for mod_ruby. */
#include "mod_ruby.h"
/*********************** internal functions *********************/
/*
search for a string in a fixed-length byte string.
if partial is true, partial matches are allowed at the end of the buffer.
returns NULL if not found, or a pointer to the start of the first match.
*/
static void* my_memstr(char* haystack, int haystacklen, const char* needle,
int partial)
{
int needlen = strlen(needle);
int len = haystacklen;
char *ptr = haystack;
/* iterate through first character matches */
while( (ptr = memchr(ptr, needle[0], len)) ) {
/* calculate length after match */
len = haystacklen - (ptr - (char *)haystack);
/* done if matches up to capacity of buffer */
if(memcmp(needle, ptr, needlen < len ? needlen : len) == 0 &&
(partial || len >= needlen))
break;
/* next character */
ptr++; len--;
}
return ptr;
}
/*
fill up the buffer with client data.
returns number of bytes added to buffer.
*/
int fill_buffer(multipart_buffer *self)
{
int bytes_to_read, actual_read = 0;
/* shift the existing data if necessary */
if(self->bytes_in_buffer > 0 && self->buf_begin != self->buffer)
memmove(self->buffer, self->buf_begin, self->bytes_in_buffer);
self->buf_begin = self->buffer;
/* calculate the free space in the buffer */
bytes_to_read = self->bufsize - self->bytes_in_buffer;
if (bytes_to_read >= self->r->remaining) {
bytes_to_read = self->r->remaining - strlen(self->boundary);
#ifdef DEBUG
ap_log_rerror(MPB_ERROR, "mozilla 0.97 hack: '%ld'", self->r->remaining);
#endif
}
/* read the required number of bytes */
if(bytes_to_read > 0) {
char *buf = self->buffer + self->bytes_in_buffer;
ap_hard_timeout("[libapreq] multipart_buffer.c:fill_buffer", self->r);
actual_read = ap_get_client_block(self->r, buf, bytes_to_read);
ap_kill_timeout(self->r);
/* update the buffer length */
if(actual_read > 0)
self->bytes_in_buffer += actual_read;
}
return actual_read;
}
/*
gets the next CRLF terminated line from the input buffer.
if it doesn't find a CRLF, and the buffer isn't completely full, returns
NULL; otherwise, returns the beginning of the null-terminated line,
minus the CRLF.
note that we really just look for LF terminated lines. this works
around a bug in internet explorer for the macintosh which sends mime
boundaries that are only LF terminated when you use an image submit
button in a multipart/form-data form.
*/
static char* next_line(multipart_buffer *self)
{
/* look for LF in the data */
char* line = self->buf_begin;
char* ptr = memchr(self->buf_begin, '\n', self->bytes_in_buffer);
/* LF found */
if(ptr) {
/* terminate the string, remove CRLF */
if((ptr - line) > 0 && *(ptr-1) == '\r') *(ptr-1) = 0;
else *ptr = 0;
/* bump the pointer */
self->buf_begin = ptr + 1;
self->bytes_in_buffer -= (self->buf_begin - line);
}
/* no LF found */
else {
/* buffer isn't completely full, fail */
if(self->bytes_in_buffer < self->bufsize)
return NULL;
/* return entire buffer as a partial line */
line[self->bufsize] = 0;
self->buf_begin = ptr;
self->bytes_in_buffer = 0;
}
return line;
}
/* returns the next CRLF terminated line from the client */
static char* get_line(multipart_buffer *self)
{
char* ptr = next_line(self);
if(!ptr) {
fill_buffer(self);
ptr = next_line(self);
}
#ifdef DEBUG
ap_log_rerror(MPB_ERROR, "get_line: '%s'", ptr);
#endif
return ptr;
}
/* finds a boundary */
static int find_boundary(multipart_buffer *self, char *boundary)
{
char *line;
/* loop thru lines */
while( (line = get_line(self)) ) {
#ifdef DEBUG
ap_log_rerror(MPB_ERROR, "find_boundary: '%s' ?= '%s'",
line, boundary);
#endif
/* finished if we found the boundary */
if(strEQ(line, boundary))
return 1;
}
/* didn't find the boundary */
return 0;
}
/*********************** external functions *********************/
/* create new multipart_buffer structure */
multipart_buffer *multipart_buffer_new(char *boundary, long length, request_rec *r)
{
multipart_buffer *self = (multipart_buffer *)
apr_pcalloc(r->pool, sizeof(multipart_buffer));
int minsize = strlen(boundary)+6;
if(minsize < FILLUNIT) minsize = FILLUNIT;
self->r = r;
self->buffer = (char *) apr_pcalloc(r->pool, minsize+1);
self->bufsize = minsize;
self->request_length = length;
self->boundary = apr_pstrcat(r->pool, "--", boundary, NULL);
self->boundary_next = apr_pstrcat(r->pool, "\n", self->boundary, NULL);
self->buf_begin = self->buffer;
self->bytes_in_buffer = 0;
return self;
}
/* parse headers and return them in an apache table */
table *multipart_buffer_headers(multipart_buffer *self)
{
table *tab;
char *line;
/* didn't find boundary, abort */
if(!find_boundary(self, self->boundary)) return NULL;
/* get lines of text, or CRLF_CRLF */
tab = apr_table_make(self->r->pool, 10);
while( (line = get_line(self)) && strlen(line) > 0 ) {
/* add header to table */
char *key = line;
char *value = strchr(line, ':');
if(value) {
*value = 0;
do { value++; } while(apr_isspace(*value));
#ifdef DEBUG
ap_log_rerror(MPB_ERROR,
"multipart_buffer_headers: '%s' = '%s'",
key, value);
#endif
apr_table_add(tab, key, value);
}
else {
#ifdef DEBUG
ap_log_rerror(MPB_ERROR,
"multipart_buffer_headers: '%s' = ''", key);
#endif
apr_table_add(tab, key, "");
}
}
return tab;
}
/* read until a boundary condition */
int multipart_buffer_read(multipart_buffer *self, char *buf, int bytes)
{
int len, max;
char *bound;
/* fill buffer if needed */
if(bytes > self->bytes_in_buffer) fill_buffer(self);
/* look for a potential boundary match, only read data up to that point */
if( (bound = my_memstr(self->buf_begin, self->bytes_in_buffer,
self->boundary_next, 1)) )
max = bound - self->buf_begin;
else
max = self->bytes_in_buffer;
/* maximum number of bytes we are reading */
len = max < bytes-1 ? max : bytes-1;
/* if we read any data... */
if(len > 0) {
/* copy the data */
memcpy(buf, self->buf_begin, len);
buf[len] = 0;
if(bound && len > 0 && buf[len-1] == '\r') buf[--len] = 0;
/* update the buffer */
self->bytes_in_buffer -= len;
self->buf_begin += len;
}
#ifdef DEBUG
ap_log_rerror(MPB_ERROR, "multipart_buffer_read: %d bytes", len);
#endif
return len;
}
/*
XXX: this is horrible memory-usage-wise, but we only expect
to do this on small pieces of form data.
*/
char *multipart_buffer_read_body(multipart_buffer *self)
{
char buf[FILLUNIT], *out = "";
while(multipart_buffer_read(self, buf, sizeof(buf)))
out = apr_pstrcat(self->r->pool, out, buf, NULL);
#ifdef DEBUG
ap_log_rerror(MPB_ERROR, "multipart_buffer_read_body: '%s'", out);
#endif
return out;
}
/* eof if we are out of bytes, or if we hit the final boundary */
int multipart_buffer_eof(multipart_buffer *self)
{
if( (self->bytes_in_buffer == 0 && fill_buffer(self) < 1) )
return 1;
else
return 0;
}
|