rsstats 0.0.1
Redis Enterprise Statistic collector
libhttp.c
Go to the documentation of this file.
1
19#include "libhttp.h"
20#include "sclist.h"
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <assert.h>
26
27typedef struct HTTPHeaders_s {
28 char* name;
29 char* value;
31
32typedef struct HTTP_s {
34 char* body;
36
38 HTTP_t* retval;
39
40 if (NULL==(retval=malloc(sizeof(struct HTTP_s)))) {
41 perror("HTTP_new");
42 exit(EXIT_FAILURE);
43 }
44
45 retval->headers = sclist_new();
46
47 if (NULL==(retval->body=malloc(1))) {
48 perror("HTTP_new empty body allocation");
49 exit(EXIT_FAILURE);
50 };
51 retval->body[0]=0;
52
53 return retval;
54}
55
56void HTTP_addheader(HTTP_t* http, const char* name, const char* value) {
57 HTTPHeaders_t* headers;
58
59 assert(http);
60 assert(http->headers);
61 assert(http->body);
62
63 if (NULL==(headers=malloc(sizeof(struct HTTPHeaders_s)))) {
64 perror("HTTP_addheader");
65 exit(EXIT_FAILURE);
66 }
67 if (NULL==(headers->name=malloc(strlen(name)+1))) {
68 perror("HTTP_addheader name malloc");
69 exit(EXIT_FAILURE);
70 }
71 strcpy(headers->name,name);
72 if (NULL==(headers->value=malloc(strlen(value)+1))) {
73 perror("HTTP_addheader value malloc");
74 exit(EXIT_FAILURE);
75 }
76 strcpy(headers->value,value);
77 sclist_addrecord(http->headers,headers);
78}
79
80char* HTTP_getheaders(const HTTP_t* http) {
81 sclistrecord_t* current = NULL;
82 char* retval;
83
84 assert(http);
85 assert(http->headers);
86 assert(http->body);
87
88 if (NULL==(retval=malloc(1))) {
89 perror("HTTP_getheaders initial malloc");
90 exit(EXIT_FAILURE);
91 };
92 retval[0]=0;
93
94 current = sclist_firstrecord(http->headers);
95 while (current) {
96 HTTPHeaders_t* header=sclist_getvalue(current);
97 if (header->name && header->value) {
98 char* newretval;
99 if (NULL==(newretval=realloc(retval,strlen(retval)+strlen(header->name)+2+strlen(header->value)+2+1))) {
100 perror("HTTP_getheaders inner loop");
101 exit(EXIT_FAILURE);
102 }
103 retval=newretval;
104 strcat(retval,header->name);
105 strcat(retval,": ");
106 strcat(retval,header->value);
107 strcat(retval,"\r\n");
108 }
109 current=sclist_nextrecord(current);
110 }
111 return retval;
112}
113
114void HTTP_del(HTTP_t* http) {
115 sclistrecord_t* current;
116
117 assert(http);
118 assert(http->headers);
119 assert(http->body);
120
121 current = sclist_firstrecord(http->headers);
122 while (current) {
123 HTTPHeaders_t* header=sclist_getvalue(current);
124 if (header->name)
125 free(header->name);
126 if (header->value)
127 free(header->value);
128 current = sclist_nextrecord(current);
129 }
130
131 free(http->body);
132 free(http);
133}
134
135char* HTTP_getrequest(const HTTPMethod_t method, const char* uri, const HTTPVersion_t version) {
136 char* retval;
137
138 assert(method<=HTTPMETHOD_INVALID);
139 assert(version<=HTTPVERSION_INVALID);
140
141 if (NULL==(retval=malloc(7+1+strlen(uri)+1+8+1))) {
142 perror("HTTP_getrequest");
143 exit(EXIT_FAILURE);
144 }
145 retval[0]=0;
146
147 switch(method) {
148 case HTTPMETHOD_GET:
149 strcat(retval, "GET");
150 break;;
151 case HTTPMETHOD_HEAD:
152 case HTTPMETHOD_POST:
153 case HTTPMETHOD_PUT:
157 case HTTPMETHOD_TRACE:
158 case HTTPMETHOD_PATCH:
160 default:
161 fprintf(stderr,"HTTP method not supported\n");
162 exit(EXIT_FAILURE);
163 break;;
164 }
165 strcat(retval," ");
166
167 /* NULL URI allowed, as an empty string */
168 if (uri)
169 strcat(retval, uri);
170
171 strcat(retval," HTTP/");
172 switch(version) {
175 strcat(retval, "1.1");
176 break;;
182 default:
183 fprintf(stderr,"HTTP version not supported\n");
184 exit(EXIT_FAILURE);
185 break;;
186 }
187 strcat(retval, "\r\n");
188 return retval;
189}
190
191void HTTP_setbody(HTTP_t* http, const char* body) {
192 char* newbody;
193
194 assert(http);
195 assert(http->body);
196
197 if (NULL==(newbody=realloc(http->body,strlen(body)+1))) {
198 perror("HTTP_setbody");
199 exit(EXIT_FAILURE);
200 }
201
202 http->body = newbody;
203 strcpy(http->body, body);
204}
205
206char* HTTP_getbody(HTTP_t* http) {
207 assert(http);
208 assert(http->body);
209
210 return http->body;
211}
const char *const name
Definition: cJSON.h:268
void HTTP_del(HTTP_t *http)
Definition: libhttp.c:114
void HTTP_addheader(HTTP_t *http, const char *name, const char *value)
Definition: libhttp.c:56
struct HTTP_s HTTP_t
HTTP_t * HTTP_new()
Definition: libhttp.c:37
struct HTTPHeaders_s HTTPHeaders_t
char * HTTP_getheaders(const HTTP_t *http)
Definition: libhttp.c:80
void HTTP_setbody(HTTP_t *http, const char *body)
Definition: libhttp.c:191
char * HTTP_getbody(HTTP_t *http)
Definition: libhttp.c:206
char * HTTP_getrequest(const HTTPMethod_t method, const char *uri, const HTTPVersion_t version)
Definition: libhttp.c:135
HTTP parsing and building library.
enum HTTPVersion_e HTTPVersion_t
@ HTTPMETHOD_PUT
Definition: libhttp.h:40
@ HTTPMETHOD_POST
Definition: libhttp.h:39
@ HTTPMETHOD_CONNECT
Definition: libhttp.h:42
@ HTTPMETHOD_TRACE
Definition: libhttp.h:44
@ HTTPMETHOD_PATCH
Definition: libhttp.h:45
@ HTTPMETHOD_DELETE
Definition: libhttp.h:41
@ HTTPMETHOD_OPTIONS
Definition: libhttp.h:43
@ HTTPMETHOD_GET
Definition: libhttp.h:37
@ HTTPMETHOD_INVALID
Definition: libhttp.h:46
@ HTTPMETHOD_HEAD
Definition: libhttp.h:38
@ HTTPVERSION_HTTP11b
Definition: libhttp.h:30
@ HTTPVERSION_INVALID
Definition: libhttp.h:33
@ HTTPVERSION_HTTP09
Definition: libhttp.h:27
@ HTTPVERSION_HTTP2
Definition: libhttp.h:31
@ HTTPVERSION_HTTP10
Definition: libhttp.h:28
@ HTTPVERSION_HTTP3
Definition: libhttp.h:32
@ HTTPVERSION_HTTP11
Definition: libhttp.h:29
enum HTTPMethod_e HTTPMethod_t
#define NULL
Definition: rsstats-opts.c:64
void * sclist_getvalue(sclistrecord_t *record)
Returns the value pointer stored in the record.
Definition: sclist.c:127
sclist_t * sclist_new()
Allocate and initialize the internal list structure.
Definition: sclist.c:39
sclistrecord_t * sclist_nextrecord(const sclistrecord_t *record)
Returns the pointer on the record following the specified one.
Definition: sclist.c:121
sclistrecord_t * sclist_addrecord(sclist_t *sclist, void *value)
Add a value at the end of the list.
Definition: sclist.c:62
sclistrecord_t * sclist_firstrecord(const sclist_t *sclist)
Returns the pointer on the first list record.
Definition: sclist.c:115
Basic single chained generic list.
Definition: libhttp.c:32
char * body
Definition: libhttp.c:34
sclist_t * headers
Definition: libhttp.c:33
char * value
Definition: libhttp.c:29
char * name
Definition: libhttp.c:28
Opaque sclist structure.
Definition: sclist.c:34
Private list record structure.
Definition: sclist.c:27