/
protochttp.c
104 lines (85 loc) · 2.58 KB
/
protochttp.c
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
/*
Copyright (C) 2012 The yummycacher Authors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <stdint.h>
#include "protochttp.h"
/* The maximum http request size. */
#define MAX_REQUEST_SIZE 10240
/* A template for the request. */
const char REQUEST[] = "GET %s HTTP/1.0\r\n"
"User-Agent: yummycacher\r\n"
"Host: archive.fedoraproject.org\r\n\r\n";
void
pchttp_parse_headers(struct ProtoCHttp *proto);
struct ProtoCHttp *
pchttp_new(void)
{
struct ProtoCHttp *proto = malloc(sizeof(*proto));
/* public */
/* private */
proto->buf = malloc(MAX_REQUEST_SIZE);
proto->buf[0] = '\0';
proto->bufsize = 0;
proto->http_length = 0;
return proto;
}
void
pchttp_free(struct ProtoCHttp *proto)
{
free(proto->buf);
free(proto);
}
int
pchttp_add_data(struct ProtoCHttp *proto, char *data, int n, char **out)
{
int n_remaining = MAX_REQUEST_SIZE - proto->bufsize - 1;
memcpy(proto->buf, data, MIN(n_remaining, n));
proto->bufsize += MIN(n_remaining, n);
proto->buf[proto->bufsize] = '\0';
char *ptr;
if ((ptr = strstr(proto->buf, "\r\n\r\n")) != NULL) {
/* parse the headers */
ptr[0] = '\0';
pchttp_parse_headers(proto);
ptr += 4;
size_t count = proto->bufsize - (ptr - proto->buf);
*out = malloc(count);
memcpy(*out, ptr, count);
return count;
}
return 0;
}
void
pchttp_parse_headers(struct ProtoCHttp *proto)
{
char **lines = g_strsplit(proto->buf, "\r\n", 0);
char **lineptr = lines;
while (*lineptr) {
if (g_str_has_prefix(*lineptr, "Content-Length:"))
sscanf(*lineptr, "Content-Length: %d", &proto->http_length);
lineptr++;
}
g_strfreev(lines);
}
char *
pchttp_get_request(struct ProtoCHttp *proto, char *location)
{
char *buf = malloc(MAX_REQUEST_SIZE);
snprintf(buf, MAX_REQUEST_SIZE, REQUEST,
location);
return buf;
}