get_body.c
1.11 KB
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
#include <stdlib.h>
#include "http/header.h"
#include "http/message.h"
#include "http/request/parser.h"
#define MAX(x,y) ((x) > (y) ? (x) : (y))
/**
* @TODO: not final...input buffer handling not final
*/
void
httpRequestParserGetBody(HttpRequestParser this)
{
HttpMessage message = (HttpMessage)(this->cur_request);
char * str_nbody;
int nbody;
int len;
str_nbody = httpHeaderGet(
&(message->header),
"Content-Length");
if (NULL == str_nbody) {
this->state = HTTP_REQUEST_DONE;
return -1;
}
nbody = atoi(str_nbody);
if (0 == message->nbody) {
message->type = HTTP_MESSAGE_BUFFERED;
if (0 < nbody)
message->body = malloc(nbody);
}
len = MAX(nbody - message->nbody, this->buffer->bused);
memcpy(message->body + message->nbody,
this->buffer->buffer + this->buffer->bstart,
len);
message->nbody += len;
this->buffer->bstart += len;
if (this->buffer->bstart >= this->buffer->bsize) {
this->buffer->bstart -= this->buffer->bsize;
}
this->buffer->bused -= len;
if (message->nbody == nbody) {
this->state = HTTP_REQUEST_DONE;
}
}
// vim: set ts=4 sw=4: