forked from jrfoell/campfire-libpurple
/
http.c
563 lines (511 loc) · 16 KB
/
http.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*local includes*/
#include "http.h"
#include "message.h"
/*system includes*/
#include <errno.h>
/*purple includes*/
#include <debug.h>
void
campfire_ssl_failure(G_GNUC_UNUSED PurpleSslConnection * gsc,
G_GNUC_UNUSED PurpleSslErrorType error,
G_GNUC_UNUSED gpointer data)
{
purple_debug_info("campfire", "ssl connect failure\n");
}
void
campfire_http_request(CampfireSslTransaction * xaction, gchar * uri,
gchar * method, xmlnode * postxml)
{
CampfireConn *conn = xaction->campfire;
const char *api_token =
purple_account_get_string(conn->account, "api_token", "");
gchar *xmlstr = NULL, *len = NULL, *encoded = NULL;
gsize auth_len;
xaction->http_request = g_string_new(method);
g_string_append(xaction->http_request, " ");
g_string_append(xaction->http_request, uri);
g_string_append(xaction->http_request, " HTTP/1.1\r\n");
g_string_append(xaction->http_request,
"Content-Type: application/xml\r\n");
g_string_append(xaction->http_request, "Authorization: Basic ");
auth_len = strlen(api_token);
encoded = purple_base64_encode((const guchar *) api_token, auth_len);
g_string_append(xaction->http_request, encoded);
g_string_append(xaction->http_request, "\r\n");
g_free(encoded);
g_string_append(xaction->http_request, "Host: ");
g_string_append(xaction->http_request, conn->hostname);
g_string_append(xaction->http_request, "\r\n");
g_string_append(xaction->http_request, "Accept: */*\r\n");
if (postxml) {
xmlstr = xmlnode_to_str(postxml, NULL);
g_string_append(xaction->http_request, "Content-Length: ");
/* len = g_strdup_printf("%lu", strlen(xmlstr)); */
len = g_strdup_printf("%" G_GSIZE_FORMAT, strlen(xmlstr));
g_string_append(xaction->http_request, len);
g_free(len);
g_string_append(xaction->http_request, "\r\n\r\n");
g_string_append(xaction->http_request, xmlstr);
g_string_append(xaction->http_request, "\r\n");
}
g_string_append(xaction->http_request, "\r\n");
purple_debug_info("campfire", "Formatted request:\n%s\n",
xaction->http_request->str);
}
/* return 1 on error, return 0 on success */
static gint
campfire_get_http_status_from_header(gint * status, GString * header)
{
gchar *str, *found_str, *extra_chars;
gchar prefix[] = "\r\nStatus: ";
gsize prefix_size = strlen(prefix);
gint tmp;
found_str = g_strstr_len(header->str, header->len, prefix);
if (!found_str) {
return 1;
}
found_str += prefix_size; /* increment pointer to start of status string */
str = g_malloc0(4); /* status is 3-digits plus NULL */
g_strlcpy(str, found_str, 4);
tmp = (gint) g_ascii_strtoull(str, &extra_chars, 10);
g_free(str);
if (tmp == 0 && extra_chars == str) {
*status = -1;
return 1;
}
*status = tmp;
return 0;
}
/* return 1 on error, return 0 on success */
static gint
campfire_get_content_length_from_header(gsize * cl, GString * header)
{
gchar *str, *found_str, *extra_chars, *eol;
gchar prefix[] = "\r\nContent-Length: ";
gsize prefix_size = strlen(prefix);
gsize rest_of_header_size;
gsize cl_size;
gsize tmp;
found_str = g_strstr_len(header->str, header->len, prefix);
if (!found_str) {
return 1;
}
found_str += prefix_size; /* increment pointer to start desired string */
rest_of_header_size = header->len - (found_str - header->str);
eol = g_strstr_len(found_str, rest_of_header_size, "\r\n");
if (!eol) {
return 1;
}
cl_size = eol - found_str;
purple_debug_info("campfire", "content length str len = %"
G_GSIZE_FORMAT "\n", cl_size);
str = g_malloc0(cl_size + 1);
g_strlcpy(str, found_str, cl_size + 1);
tmp = g_ascii_strtoll(str, &extra_chars, 10);
g_free(str);
if (tmp == 0 && extra_chars == str) {
*cl = 0;
return 1;
}
purple_debug_info("campfire", "content length: %" G_GSIZE_FORMAT "\n",
tmp);
*cl = tmp;
return 0;
}
static gboolean
ssl_input_consumed(GString * ssl_input)
{
gboolean consumed = FALSE;
if (ssl_input == NULL) {
consumed = TRUE;
} else if (ssl_input->len == 0) {
consumed = TRUE;
}
return consumed;
}
static void
campfire_consume_http_header(CampfireHttpResponse * response,
GString * ssl_input)
{
gchar blank_line[] = "\r\n\r\n";
gchar *header_end;
header_end = g_strstr_len(ssl_input->str, ssl_input->len, blank_line);
gsize header_len;
if (header_end) {
header_end += strlen(blank_line);
header_len = header_end - ssl_input->str;
} else {
header_len = ssl_input->len;
}
g_string_append_len(response->response, ssl_input->str, header_len);
g_string_erase(ssl_input, 0, header_len);
}
static gboolean
campfire_http_header_received(CampfireHttpResponse * response)
{
gboolean received = FALSE;
gchar blank_line[] = "\r\n\r\n";
gchar *header_end;
header_end =
g_strstr_len(response->response->str, response->response->len,
blank_line);
if (header_end) {
received = TRUE;
}
return received;
}
/* this function should be called just after the header (including blank line
* have been copied from ssl_input to the response string. That way this
* operation to save the header string becomes a simple string copy from
* one GString to another.
*/
static void
campfire_process_http_header(CampfireHttpResponse * response)
{
response->header = g_string_new("");
g_string_append(response->header, response->response->str);
campfire_get_http_status_from_header(&response->status,
response->header);
campfire_get_content_length_from_header(&response->content_len,
response->header);
}
static void
campfire_consume_http_content(CampfireHttpResponse * response,
GString * ssl_input)
{
g_string_append_len(response->response, ssl_input->str, ssl_input->len);
response->content_received_len += ssl_input->len;
g_string_erase(ssl_input, 0, -1);
}
static gboolean
campfire_http_content_received(CampfireHttpResponse * response)
{
return response->content_received_len >= response->content_len;
}
/* this function should be called just after the received bytes match the
* content length prescribe in the http header. To create the 'content'
* GString we must copy the 'response' string starting immediately after
* the blank line
*/
static void
campfire_process_http_content(CampfireHttpResponse * response)
{
gsize content_start_index = response->header->len;
response->content = g_string_new("");
g_string_append(response->content,
&response->response->str[content_start_index]);
}
gint
campfire_http_response(PurpleSslConnection * gsc,
CampfireSslTransaction * xaction,
G_GNUC_UNUSED PurpleInputCondition cond,
G_GNUC_UNUSED xmlnode ** node)
{
gchar buf[1024];
GString *ssl_input;
gint len, errsv = 0;
gint status;
CampfireHttpResponse *response = &xaction->http_response;
if (response->rx_state == CAMPFIRE_HTTP_RX_DONE) {
purple_debug_info("campfire", "somefin aint right.\n");
return -1;
}
/**********************************************************************
* read input from file descriptor
*********************************************************************/
ssl_input = g_string_new("");
errno = 0;
while ((len = purple_ssl_read(gsc, buf, sizeof(buf))) > 0) {
purple_debug_info("campfire",
"read %d bytes from HTTP Response, errno: %i\n",
len, errno);
ssl_input = g_string_append_len(ssl_input, buf, len);
}
errsv = errno;
/**********************************************************************
* handle return value of ssl input read
*********************************************************************/
if (len < 0 && errsv == EAGAIN) {
if (ssl_input->len == 0) {
purple_debug_info("campfire",
"TRY AGAIN (returning)\n");
g_string_free(ssl_input, TRUE);
return 0;
} else {
purple_debug_info("campfire", "EAGAIN (continuing)\n");
}
} else if (len == 0) {
purple_debug_info("campfire", "SERVER CLOSED CONNECTION\n");
if (ssl_input->len == 0) {
g_string_free(ssl_input, TRUE);
return -1;
}
} else {
purple_debug_info("campfire", "LOST CONNECTION\n");
purple_debug_info("campfire", "errno: %d\n", errsv);
g_string_free(ssl_input, TRUE);
return -1;
}
if (!response->response) {
response->response = g_string_new("");
}
purple_debug_info("campfire", "ssl_input:\n%s", ssl_input->str);
/**********************************************************************
* process input with a simple state machine
*********************************************************************/
while (!ssl_input_consumed(ssl_input)) {
switch (response->rx_state) {
case CAMPFIRE_HTTP_RX_HEADER:
purple_debug_info("campfire",
"CAMPFIRE_HTTP_RX_HEADER\n");
campfire_consume_http_header(response, ssl_input);
if (campfire_http_header_received(response)) {
campfire_process_http_header(response);
response->rx_state = CAMPFIRE_HTTP_RX_CONTENT;
}
break;
case CAMPFIRE_HTTP_RX_CONTENT:
purple_debug_info("campfire",
"CAMPFIRE_HTTP_RX_CONTENT\n");
campfire_consume_http_content(response, ssl_input);
if (campfire_http_content_received(response)) {
campfire_process_http_content(response);
response->rx_state = CAMPFIRE_HTTP_RX_DONE;
}
break;
case CAMPFIRE_HTTP_RX_DONE:
purple_debug_info("campfire",
"CAMPFIRE_HTTP_RX_DONE\n");
g_string_erase(ssl_input, 0, -1); /* consume input */
break;
default:
g_string_erase(ssl_input, 0, -1); /* consume input */
break;
}
}
/**********************************************************************
* return http status code: -1=error, 0=input_not_received
*********************************************************************/
if (response->rx_state == CAMPFIRE_HTTP_RX_DONE) {
status = response->status;
} else {
status = 0;
}
g_string_free(ssl_input, TRUE);
return status;
}
void
campfire_message_free(gpointer data)
{
CampfireMessage *msg = (CampfireMessage *)(data);
if (msg != NULL) {
if (msg->id != NULL) {
g_free(msg->id);
}
if (msg->type != NULL) {
g_free(msg->type);
}
if (msg->message != NULL) {
g_free(msg->message);
}
if (msg->user_id != NULL) {
g_free(msg->user_id);
}
g_free(msg);
}
}
void
campfire_xaction_free(CampfireSslTransaction *xaction)
{
if (xaction) {
if (xaction->http_request) {
g_string_free(xaction->http_request, TRUE);
}
if (xaction->http_response.response) {
g_string_free(xaction->http_response.response,
TRUE);
}
if (xaction->http_response.header) {
g_string_free(xaction->http_response.header,
TRUE);
}
if (xaction->http_response.content) {
g_string_free(xaction->http_response.content,
TRUE);
}
if (xaction->xml_response) {
xmlnode_free(xaction->xml_response);
}
if (xaction->room_id) {
g_free(xaction->room_id);
}
g_list_free_full(xaction->messages, &campfire_message_free);
xaction->campfire->num_xaction_free++; /* valgrind investigation */
purple_debug_info("campfire", "xaction: %p, num_xaction_malloc:%d: num_xaction_free:%d\n",
xaction,
xaction->campfire->num_xaction_malloc,
xaction->campfire->num_xaction_free);
g_free(xaction);
}
}
void
campfire_ssl_handler(CampfireConn * campfire,
PurpleSslConnection * gsc, PurpleInputCondition cond)
{
GList *first = g_list_first(campfire->queue);
CampfireSslTransaction *xaction = NULL;
gint status;
gboolean close_ssl = FALSE;
gboolean cleanup = TRUE;
if (first) {
xaction = first->data;
} else {
xaction = g_new0(CampfireSslTransaction, 1);
campfire->num_xaction_malloc++; /* valgrind investigation */
purple_debug_info("campfire", "%s: xaction: %p, campfire->num_xaction_malloc:%d\n",
__FUNCTION__, xaction, campfire->num_xaction_malloc);
xaction->campfire = campfire;
}
purple_debug_info("campfire", "%s: first: %p\n", __FUNCTION__, first);
status = campfire_http_response(gsc, xaction, cond,
&(xaction->xml_response));
purple_debug_info("campfire", "http status: %d\n", status);
if (status == 200 || status == 201) {
xaction->xml_response =
xmlnode_from_str(xaction->http_response.content->str,
-1);
if (xaction && xaction->response_cb) {
purple_debug_info("campfire",
"calling response_cb (%p)\n",
xaction->response_cb);
xaction->response_cb(xaction, gsc, cond);
}
cleanup = TRUE;
} else if (status == 0) { /*received partial content */
cleanup = FALSE;
} else { /*status < 0 or some other http status we don't expect */
close_ssl = TRUE;
cleanup = TRUE;
}
if (close_ssl && campfire->gsc) {
purple_debug_info("campfire",
"closing ssl connection:%p (%p)\n", gsc,
campfire->gsc);
campfire->gsc = NULL;
purple_ssl_close(gsc);
cleanup = TRUE;
}
if (cleanup) {
campfire_xaction_free(xaction);
if (first) {
purple_debug_info("campfire",
"removing from queue: length: %d\n",
g_list_length(campfire->queue));
campfire->queue =
g_list_remove(campfire->queue, xaction);
purple_debug_info("campfire",
"removed from queue: length: %d\n",
g_list_length(campfire->queue));
}
first = g_list_first(campfire->queue);
if (first) {
xaction = first->data;
purple_debug_info("campfire",
"writing subsequent request on ssl connection\n");
purple_ssl_write(gsc, xaction->http_request->str,
xaction->http_request->len);
}
}
}
/*
* this prototype is needed because the next two functions call each other.
*/
static void campfire_ssl_connect(CampfireConn * campfire,
PurpleInputCondition cond,
gboolean from_connection_callback);
/* This is just an itermediate function:
* It is called by lower level purple stuff when the ssl connection is
* established. It merely calls 'campfire_ssl_connect' with an extra argument
* to denote that it's coming from the callback.
*/
static void
campfire_ssl_connect_cb(CampfireConn * campfire, PurpleInputCondition cond)
{
campfire_ssl_connect(campfire, cond, TRUE);
}
static void
campfire_ssl_connect(CampfireConn * campfire,
G_GNUC_UNUSED PurpleInputCondition cond,
gboolean from_connection_callback)
{
GList *first = NULL;
CampfireSslTransaction *xaction = NULL;
purple_debug_info("campfire", "%s\n", __FUNCTION__);
if (!campfire) {
return;
} else {
first = g_list_first(campfire->queue);
}
if (!first) {
return;
} else {
xaction = first->data;
}
if (!xaction) {
return;
}
if (!campfire->gsc) {
purple_debug_info("campfire", "new ssl connection\n");
campfire->gsc = purple_ssl_connect(campfire->account,
campfire->hostname,
443, (PurpleSslInputFunction)
(campfire_ssl_connect_cb),
campfire_ssl_failure,
campfire);
purple_debug_info("campfire",
"new ssl connection kicked off.\n");
} else {
purple_debug_info("campfire", "previous ssl connection\n");
/* we want to write our http request to the ssl connection
* WHENEVER this is called from the callback (meaning we've
* JUST NOW established the connection). OR when the first
* transaction is added to the queue on an OPEN ssl connection
*/
if (from_connection_callback
|| g_list_length(campfire->queue) == 1) {
/* campfire_ssl_handler is the ONLY input handler we
* EVER use So... if there is already an input handler
* present (inpa > 0), then we DON"T want to add another
* input handler. Quite a few hours spent chasing bugs
* when multiple input handlers were added!
*/
if (campfire->gsc->inpa == 0) {
purple_debug_info("campfire", "adding input\n");
purple_ssl_input_add(campfire->gsc,
(PurpleSslInputFunction)
(campfire_ssl_handler),
campfire);
}
purple_debug_info("campfire",
"writing first request on ssl connection\n");
purple_ssl_write(campfire->gsc,
xaction->http_request->str,
xaction->http_request->len);
}
}
return;
}
void
campfire_queue_xaction(CampfireConn * campfire,
CampfireSslTransaction * xaction,
PurpleInputCondition cond)
{
gboolean from_callback = FALSE; /* this is not the ssl connection callback */
purple_debug_info("campfire", "%s input condition: %i\n", __FUNCTION__,
cond);
xaction->queued = TRUE;
campfire->queue = g_list_append(campfire->queue, xaction);
purple_debug_info("campfire", "queue length %d\n",
g_list_length(campfire->queue));
campfire_ssl_connect(campfire, cond, from_callback);
}