read.c
1 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
#include <stdlib.h>
#include <unistd.h>
#include "http/request/parser.h"
size_t
httpRequestParserRead(HttpRequestParser this, int fd)
{
size_t remaining, chunks;
char buffer[1024];
size_t size = read(fd, buffer, 1024);
if (0 < size) {
remaining = this->buffer_used % HTTP_REQUEST_PARSER_READ_CHUNK;
chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK;
/**
* because a division always rounds down
* chunks holds exactly the currently allocated chunks if
* remaining equals 0 but there is no space left.
* Else chunks holds the actually allocated amount of chunks
* minus 1.
* For this reason chunks always has to be increased by 1.
*/
chunks++;
if (size >= remaining) {
this->buffer =
realloc(this->buffer, chunks * HTTP_REQUEST_PARSER_READ_CHUNK);
}
memcpy(this->buffer + this->buffer_used, buffer, size);
this->buffer_used += size;
this->buffer[this->buffer_used] = 0;
size = httpRequestParserParse(this);
}
return size;
}
// vim: set ts=4 sw=4: