diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 123 | 
1 files changed, 64 insertions, 59 deletions
| @@ -112,8 +112,6 @@ void * next_customer(size_t new_socket) { | |||
| 112 | char * key = output_buffer; | 112 | char * key = output_buffer; | 
| 113 | 113 | ||
| 114 | char * saveptr = NULL; | 114 | char * saveptr = NULL; | 
| 115 | char * content_start = NULL; | ||
| 116 | char * content_end = NULL; | ||
| 117 | 115 | ||
| 118 | char * start = output_buffer; | 116 | char * start = output_buffer; | 
| 119 | char * end = NULL; | 117 | char * end = NULL; | 
| @@ -166,9 +164,6 @@ void * next_customer(size_t new_socket) { | |||
| 166 | end[0] = '\0'; | 164 | end[0] = '\0'; | 
| 167 | search = ":"; // we will continue to search for headers | 165 | search = ":"; // we will continue to search for headers | 
| 168 | 166 | ||
| 169 | if(http_header.boundary && http_header.boundary_size == 0) { | ||
| 170 | http_header.boundary_size = strlen(http_header.boundary); | ||
| 171 | } | ||
| 172 | 167 | ||
| 173 | if(NULL == name) { | 168 | if(NULL == name) { | 
| 174 | if(NULL == http_header.method) { | 169 | if(NULL == http_header.method) { | 
| @@ -187,12 +182,65 @@ void * next_customer(size_t new_socket) { | |||
| 187 | verbose("\033[32m[%ld]> '% 20s' = '%s'\033[0m\n", matchlen, name, start); | 182 | verbose("\033[32m[%ld]> '% 20s' = '%s'\033[0m\n", matchlen, name, start); | 
| 188 | 183 | ||
| 189 | if(matchlen > http_header.newline_length) { | 184 | if(matchlen > http_header.newline_length) { | 
| 190 | verbose("END HEADERS, boundary='%s'[%ld]\n", http_header.boundary, http_header.boundary_size); | 185 | verbose("> END HEADERS, because there were %d newlines; boundary='%s'[%ld]\n", matchlen / http_header.newline_length, http_header.boundary, http_header.boundary_size); | 
| 191 | search = "-"; | 186 | end += matchlen; | 
| 192 | } | 187 | |
| 193 | break; | 188 | // if it was the first header, we calculate the boundary size and expect more headers to come after a boundary | 
| 194 | case '-': | 189 | if(http_header.boundary && http_header.boundary_size == 0) { | 
| 195 | { | 190 | verbose("================================================================================\n"); | 
| 191 | http_header.boundary_size = strlen(http_header.boundary); | ||
| 192 | // skip the first header and boundary... | ||
| 193 | start = end; | ||
| 194 | start += strspn(start, "-"); | ||
| 195 | start += http_header.boundary_size; | ||
| 196 | start += http_header.newline_length; | ||
| 197 | continue; | ||
| 198 | } else { | ||
| 199 | char * content_start = end; | ||
| 200 | while(1) | ||
| 201 | { | ||
| 202 | size_t size_remaining = (size_t) output_buffer_length - (end - output_buffer) - 1; | ||
| 203 | verbose("%ld remaining.\n", size_remaining); fflush(stdout); | ||
| 204 | |||
| 205 | if(size_remaining <= 0) { | ||
| 206 | verbose("> not even the boundary would fit in that what is left.\n"); | ||
| 207 | break; | ||
| 208 | } | ||
| 209 | |||
| 210 | if(NULL == (end = memchr((void*) end, '-', size_remaining))) { | ||
| 211 | verbose("no further '-' found\n"); | ||
| 212 | break; | ||
| 213 | } | ||
| 214 | |||
| 215 | char * content_end = end - http_header.newline_length; | ||
| 216 | |||
| 217 | |||
| 218 | end += strspn(end, "-"); | ||
| 219 | if(0 == strncmp(end, http_header.boundary, http_header.boundary_size)) { | ||
| 220 | size_t file_size = content_end - content_start; | ||
| 221 | verbose("> Content ends here, size of the last file is %ld: {begin}", file_size); | ||
| 222 | fwrite(content_start, file_size, 1, stdout); | ||
| 223 | verbose("{end}\n"); | ||
| 224 | |||
| 225 | |||
| 226 | // puts(" <[last 20 bytes]\n"); | ||
| 227 | |||
| 228 | |||
| 229 | end += http_header.boundary_size; | ||
| 230 | matchlen = strspn(end, "\r\n"); | ||
| 231 | // matchlen = http_header.boundary_size; | ||
| 232 | verbose("> end is at %p, matchlen is %ld\n", end, matchlen); | ||
| 233 | |||
| 234 | |||
| 235 | search = ":"; | ||
| 236 | break; | ||
| 237 | } else { | ||
| 238 | // printf("[%ld] kack: %c\n", size_remaining, p[0]); fflush(stdout); | ||
| 239 | end = end + 1; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | /* | ||
| 196 | char * content_start = end; | 244 | char * content_start = end; | 
| 197 | content_start += strspn(content_start, "-"); | 245 | content_start += strspn(content_start, "-"); | 
| 198 | // verbose("CONTENT: %s\n", content_start); | 246 | // verbose("CONTENT: %s\n", content_start); | 
| @@ -205,64 +253,21 @@ void * next_customer(size_t new_socket) { | |||
| 205 | } else { | 253 | } else { | 
| 206 | verbose("NO MATCH\n"); | 254 | verbose("NO MATCH\n"); | 
| 207 | } | 255 | } | 
| 256 | */ | ||
| 208 | break; | 257 | break; | 
| 209 | } | 258 | } | 
| 210 | } | 259 | } | 
| 211 | 260 | ||
| 261 | // verbose("> end is at %p, matchlen is %ld\n", end, matchlen); | ||
| 212 | start = end + matchlen; | 262 | start = end + matchlen; | 
| 263 | // fwrite(start, 42, 1, stdout); | ||
| 264 | // printf(" <[first 42 bytes of new start; matchlen was %ld; searching for %s\n", matchlen, search); | ||
| 213 | } | 265 | } | 
| 214 | printf("last known position: %p / %p with %s\n", start, end, search); | 266 | printf("last known position: %p / %p with %s\n", start, end, search); | 
| 215 | 267 | ||
| 216 | |||
| 217 | char * p; | ||
| 218 | if(NULL != (p = strstr(start, http_header.boundary))) { | ||
| 219 | puts("OKAY?"); | ||
| 220 | } | ||
| 221 | |||
| 222 | p = start; | ||
| 223 | while(1) { | ||
| 224 | size_t size_remaining = (size_t) output_buffer_length - (p - output_buffer) - 1; | ||
| 225 | printf("%ld remaining.\n", size_remaining); fflush(stdout); | ||
| 226 | |||
| 227 | if(size_remaining <= 0) { | ||
| 228 | verbose("> not even the boundary would fit in that what is left.\n"); | ||
| 229 | break; | ||
| 230 | } | ||
| 231 | |||
| 232 | p = (char *) memchr( | ||
| 233 | (void *) p, '-', size_remaining); | ||
| 234 | |||
| 235 | |||
| 236 | if(!p) { | ||
| 237 | verbose("> no further minus found.\n"); | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | |||
| 241 | end = p; | ||
| 242 | if(end[-1] == '\n') end--; | ||
| 243 | if(end[-1] == '\r') end--; | ||
| 244 | p += strspn(p, "-"); | ||
| 245 | |||
| 246 | if(0 == strncmp(p, http_header.boundary, http_header.boundary_size)) { | ||
| 247 | verbose("> Content ends here, size of the last file is %ld\n", end - start); | ||
| 248 | |||
| 249 | fwrite(start, 20, 1, stdout); | ||
| 250 | puts(" <[first 20 bytes]\n"); | ||
| 251 | fwrite(end - 20, 20, 1, stdout); | ||
| 252 | puts(" <[last 20 bytes]\n"); | ||
| 253 | break; | ||
| 254 | } else { | ||
| 255 | // printf("[%ld] kack: %c\n", size_remaining, p[0]); fflush(stdout); | ||
| 256 | p = p + 1; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | start = p; | ||
| 260 | // exit(0); | ||
| 261 | |||
| 262 | if(http_header.boundary) | 268 | if(http_header.boundary) | 
| 263 | verbose("http-boundary: %s", http_header.boundary); | 269 | verbose("http-boundary: %s", http_header.boundary); | 
| 264 | 270 | ||
| 265 | |||
| 266 | verbose("> sending answer..."); | 271 | verbose("> sending answer..."); | 
| 267 | send_answer(f_r); | 272 | send_answer(f_r); | 
| 268 | fclose(f_r); | 273 | fclose(f_r); | 
| @@ -495,4 +500,4 @@ void * next_customer(void * new_socket) { | |||
| 495 | return NULL; | 500 | return NULL; | 
| 496 | } | 501 | } | 
| 497 | */ | 502 | */ | 
| 498 | // vim: shiftwidth=2 tabstop=2 number | 503 | // vim: shiftwidth=2 tabstop=2 number foldmethod=marker | 
