-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-utils.c
110 lines (82 loc) · 2.23 KB
/
post-utils.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "str-utils.h"
#include "post-utils.h"
/* -------------------------------------------------------------------------- */
int
post_query_init(post_query_t self)
{
self->query_stream.sep = '&';
return 0;
}
/* -------------------------------------------------------------------------- */
void
post_query_release(post_query_t self)
{
if (self->data)
free(self->data);
}
/* -------------------------------------------------------------------------- */
int
post_query_feed_header(post_query_t self, const char * key, const char * value)
{
if ( strcmp(key, "content-length")==0 )
{
self->content_length = strtol(value, NULL, 10);
return 0;
}
return 1;
}
/* -------------------------------------------------------------------------- */
int
post_query_feed_data(post_query_t self, const char * data, int len)
{
char * p_data;
if ( (p_data = str_ncat(self->data, data, len)) != NULL )
{
fprintf(stderr, "post_query_feed_data (%d) : %s\n", len, p_data);
if (self->content_length < len)
return -1;
self->content_length -= len;
self->data_size += len;
self->data = p_data;
query_stream_reset(&self->query_stream);
/*
memset(&self->query_stream, 0, sizeof(struct query_stream));
q->sep = '&'
*/
return 0;
}
else
return -1;
}
/* -------------------------------------------------------------------------- */
int
post_query_read(post_query_t self, post_query_on_pair_t on_pair, void * u_ptr)
{
query_stream_t q = &self->query_stream;
int result = 1,
res, /* parser result */
read = 0,
total = self->data_size;
char * data = self->data;
result = 1;
fprintf(stderr, ": DATA: %s, %u, %u\n", data, total, self->content_length);
while (
(res = query_stream_read(q, data, total, (self->content_length == 0))) > 0
)
{
result = on_pair(q, u_ptr);
fprintf(stderr, ": res=%d, result=%d\n", res, result);
read = res;
if (result != 0)
break;
}
total -= read;
if (total)
memmove(data, &data[read], total);
data[total] = 0;
return result;
}
/* -------------------------------------------------------------------------- */