| 27 */ |
27 */ |
| 28 |
28 |
| 29 #include "httpclient.h" |
29 #include "httpclient.h" |
| 30 |
30 |
| 31 #include <cx/buffer.h> |
31 #include <cx/buffer.h> |
| |
32 #include <stdlib.h> |
| |
33 #include <string.h> |
| 32 |
34 |
| 33 static int client_connected(EventHandler *ev, Event *event); |
35 static int client_connected(EventHandler *ev, Event *event); |
| 34 |
36 |
| 35 HttpClient* http_client_new(EventHandler *ev) { |
37 HttpClient* http_client_new(EventHandler *ev) { |
| 36 CxMempool *mp = cxMempoolCreate(32, CX_MEMPOOL_TYPE_PURE); |
38 CxMempool *mp = cxMempoolCreate(32, CX_MEMPOOL_TYPE_PURE); |
| 52 memset(client, 0, sizeof(HttpClient)); |
54 memset(client, 0, sizeof(HttpClient)); |
| 53 client->request_headers = req_headers; |
55 client->request_headers = req_headers; |
| 54 client->response_headers = resp_headers; |
56 client->response_headers = resp_headers; |
| 55 |
57 |
| 56 return client; |
58 return client; |
| |
59 } |
| |
60 |
| |
61 void http_client_free(HttpClient *client) { |
| |
62 cxMempoolFree(client->mp); |
| |
63 header_array_free(client->request_headers); |
| |
64 free(client->addr); |
| |
65 free(client->method); |
| |
66 free(client->uri); |
| |
67 free(client); |
| 57 } |
68 } |
| 58 |
69 |
| 59 int http_client_set_addr(HttpClient *client, const struct sockaddr *addr, socklen_t addrlen) { |
70 int http_client_set_addr(HttpClient *client, const struct sockaddr *addr, socklen_t addrlen) { |
| 60 free(client->addr); |
71 free(client->addr); |
| 61 client->addr = NULL; |
72 client->addr = NULL; |
| 70 client->addrlen = addrlen; |
81 client->addrlen = addrlen; |
| 71 |
82 |
| 72 return 0; |
83 return 0; |
| 73 } |
84 } |
| 74 |
85 |
| |
86 int http_client_set_method(HttpClient *client, const char *method) { |
| |
87 return http_client_set_method_len(client, method, method ? strlen(method) : 0); |
| |
88 } |
| |
89 |
| |
90 int http_client_set_uri(HttpClient *client, const char *uri) { |
| |
91 return http_client_set_uri_len(client, uri, uri ? strlen(uri) : 0); |
| |
92 } |
| |
93 |
| |
94 static int client_set_str(char **ptr, const char *str, size_t len) { |
| |
95 free(*ptr); |
| |
96 if(str) { |
| |
97 char *newvalue = malloc(len+1); |
| |
98 if(!newvalue) { |
| |
99 *ptr = NULL; |
| |
100 return 1; |
| |
101 } |
| |
102 memcpy(newvalue, str, len); |
| |
103 newvalue[len] = 0; |
| |
104 *ptr = newvalue; |
| |
105 } else { |
| |
106 *ptr = NULL; |
| |
107 return 0; |
| |
108 } |
| |
109 } |
| |
110 |
| |
111 int http_client_set_method_len(HttpClient *client, const char *method, size_t len) { |
| |
112 return client_set_str(&client->method, method, len); |
| |
113 } |
| |
114 |
| |
115 int http_client_set_uri_len(HttpClient *client, const char *uri, size_t len) { |
| |
116 return client_set_str(&client->uri, uri, len); |
| |
117 } |
| |
118 |
| 75 int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value) { |
119 int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value) { |
| 76 return header_array_add(client->request_headers, name, value); |
120 return header_array_add(client->request_headers, name, value); |
| 77 } |
121 } |
| 78 |
122 |
| 79 int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value) { |
123 int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value) { |
| 111 |
155 |
| 112 int ret = ev_pollout(client->ev, socketfd, &client->writeev); |
156 int ret = ev_pollout(client->ev, socketfd, &client->writeev); |
| 113 if(ret) { |
157 if(ret) { |
| 114 close(socketfd); |
158 close(socketfd); |
| 115 } |
159 } |
| 116 return 1; |
160 return ret; |
| 117 } |
161 } |
| 118 |
162 |
| 119 static int create_req_buffer(HttpClient *client) { |
163 static int create_req_buffer(HttpClient *client) { |
| 120 CxBuffer buf; |
164 CxBuffer buf; |
| 121 if(cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_AUTO_EXTEND)) { |
165 if(cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_AUTO_EXTEND)) { |