Simplify the set_log_callback, it is better for thread safety
[deb_shairplay.git] / src / lib / http_response.c
1 /**
2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "http_response.h"
21 #include "compat.h"
22
23 struct http_response_s {
24 int complete;
25
26 char *data;
27 int data_size;
28 int data_length;
29 };
30
31
32 static void
33 http_response_add_data(http_response_t *response, const char *data, int datalen)
34 {
35 int newdatasize;
36
37 assert(response);
38 assert(data);
39 assert(datalen > 0);
40
41 newdatasize = response->data_size;
42 while (response->data_size+datalen > newdatasize) {
43 newdatasize *= 2;
44 }
45 if (newdatasize != response->data_size) {
46 response->data = realloc(response->data, newdatasize);
47 assert(response->data);
48 }
49 memcpy(response->data+response->data_length, data, datalen);
50 response->data_length += datalen;
51 }
52
53 http_response_t *
54 http_response_init(const char *protocol, int code, const char *message)
55 {
56 http_response_t *response;
57 char codestr[4];
58
59 assert(code >= 100 && code < 1000);
60
61 /* Convert code into string */
62 memset(codestr, 0, sizeof(codestr));
63 snprintf(codestr, sizeof(codestr), "%u", code);
64
65 response = calloc(1, sizeof(http_response_t));
66 if (!response) {
67 return NULL;
68 }
69
70 /* Allocate response data */
71 response->data_size = 1024;
72 response->data = malloc(response->data_size);
73 if (!response->data) {
74 free(response);
75 return NULL;
76 }
77
78 /* Add first line of response to the data array */
79 http_response_add_data(response, protocol, strlen(protocol));
80 http_response_add_data(response, " ", 1);
81 http_response_add_data(response, codestr, strlen(codestr));
82 http_response_add_data(response, " ", 1);
83 http_response_add_data(response, message, strlen(message));
84 http_response_add_data(response, "\r\n", 2);
85
86 return response;
87 }
88
89 void
90 http_response_destroy(http_response_t *response)
91 {
92 if (response) {
93 free(response->data);
94 free(response);
95 }
96 }
97
98 void
99 http_response_add_header(http_response_t *response, const char *name, const char *value)
100 {
101 assert(response);
102 assert(name);
103 assert(value);
104
105 http_response_add_data(response, name, strlen(name));
106 http_response_add_data(response, ": ", 2);
107 http_response_add_data(response, value, strlen(value));
108 http_response_add_data(response, "\r\n", 2);
109 }
110
111 void
112 http_response_finish(http_response_t *response, const char *data, int datalen)
113 {
114 assert(response);
115 assert(datalen==0 || (data && datalen > 0));
116
117 if (data && datalen > 0) {
118 const char *hdrname = "Content-Length";
119 char hdrvalue[16];
120
121 memset(hdrvalue, 0, sizeof(hdrvalue));
122 snprintf(hdrvalue, sizeof(hdrvalue)-1, "%d", datalen);
123
124 /* Add Content-Length header first */
125 http_response_add_data(response, hdrname, strlen(hdrname));
126 http_response_add_data(response, ": ", 2);
127 http_response_add_data(response, hdrvalue, strlen(hdrvalue));
128 http_response_add_data(response, "\r\n\r\n", 4);
129
130 /* Add data to the end of response */
131 http_response_add_data(response, data, datalen);
132 } else {
133 /* Add extra end of line after headers */
134 http_response_add_data(response, "\r\n", 2);
135 }
136 response->complete = 1;
137 }
138
139 const char *
140 http_response_get_data(http_response_t *response, int *datalen)
141 {
142 assert(response);
143 assert(datalen);
144 assert(response->complete);
145
146 *datalen = response->data_length;
147 return response->data;
148 }