forked from FDH2/UxPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_response.c
183 lines (155 loc) · 5.03 KB
/
http_response.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
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
/**
* Copyright (C) 2011-2012 Juho Vähä-Herttua
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "http_response.h"
#include "compat.h"
struct http_response_s {
int complete;
int disconnect;
char *data;
int data_size;
int data_length;
};
static void
http_response_add_data(http_response_t *response, const char *data, int datalen)
{
int newdatasize;
assert(response);
assert(data);
assert(datalen > 0);
newdatasize = response->data_size;
while (response->data_size+datalen > newdatasize) {
newdatasize *= 2;
}
if (newdatasize != response->data_size) {
response->data = realloc(response->data, newdatasize);
assert(response->data);
}
memcpy(response->data+response->data_length, data, datalen);
response->data_length += datalen;
}
http_response_t *
http_response_create()
{
http_response_t *response = (http_response_t *) calloc(1, sizeof(http_response_t));
if (!response) {
return NULL;
}
/* Allocate response data */
response->data_size = 1024;
response->data = (char *) malloc(response->data_size);
if (!response->data) {
free(response);
return NULL;
}
return response;
}
void
http_response_init(http_response_t *response, const char *protocol, int code, const char *message)
{
assert(response);
response->data_length = 0; /* can be used to reinitialize a previously-initialized response */
char codestr[4];
assert(code >= 100 && code < 1000);
/* Convert code into string */
memset(codestr, 0, sizeof(codestr));
snprintf(codestr, sizeof(codestr), "%u", code);
/* Add first line of response to the data array */
http_response_add_data(response, protocol, strlen(protocol));
http_response_add_data(response, " ", 1);
http_response_add_data(response, codestr, strlen(codestr));
http_response_add_data(response, " ", 1);
http_response_add_data(response, message, strlen(message));
http_response_add_data(response, "\r\n", 2);
}
void
http_response_destroy(http_response_t *response)
{
if (response) {
free(response->data);
free(response);
}
}
void
http_response_add_header(http_response_t *response, const char *name, const char *value)
{
assert(response);
assert(name);
assert(value);
http_response_add_data(response, name, strlen(name));
http_response_add_data(response, ": ", 2);
http_response_add_data(response, value, strlen(value));
http_response_add_data(response, "\r\n", 2);
}
void
http_response_finish(http_response_t *response, const char *data, int datalen)
{
assert(response);
assert(datalen==0 || (data && datalen > 0));
if (data && datalen > 0) {
const char *hdrname = "Content-Length";
char hdrvalue[16];
memset(hdrvalue, 0, sizeof(hdrvalue));
snprintf(hdrvalue, sizeof(hdrvalue)-1, "%d", datalen);
/* Add Content-Length header first */
http_response_add_data(response, hdrname, strlen(hdrname));
http_response_add_data(response, ": ", 2);
http_response_add_data(response, hdrvalue, strlen(hdrvalue));
http_response_add_data(response, "\r\n\r\n", 4);
/* Add data to the end of response */
http_response_add_data(response, data, datalen);
} else {
/* check for "Content-Type" header, with datalen = 0 */
const char *item = "Content-Type";
int item_len = strlen(item);
const char *part = response->data;
int end = response->data_length - item_len;
for (int i = 0; i < end; i++) {
if (memcmp (part, item, item_len) == 0) {
const char *hdrname = "Content-Length: 0";
http_response_add_data(response, hdrname, strlen(hdrname));
http_response_add_data(response, "\r\n", 2);
break;
}
part++;
}
/* Add extra end of line after headers */
http_response_add_data(response, "\r\n", 2);
}
response->complete = 1;
}
void
http_response_set_disconnect(http_response_t *response, int disconnect)
{
assert(response);
response->disconnect = !!disconnect;
}
int
http_response_get_disconnect(http_response_t *response)
{
assert(response);
return response->disconnect;
}
const char *
http_response_get_data(http_response_t *response, int *datalen)
{
assert(response);
assert(datalen);
assert(response->complete);
*datalen = response->data_length;
return response->data;
}