This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
request.c
135 lines (108 loc) · 3.05 KB
/
request.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
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "queue.h"
#include "simplehttp.h"
#include "request.h"
#include "stat.h"
extern int verbose;
struct simplehttp_request *simplehttp_request_new(struct evhttp_request *req, uint64_t id)
{
struct simplehttp_request *s_req;
simplehttp_ts start_ts;
simplehttp_ts_get(&start_ts);
s_req = malloc(sizeof(struct simplehttp_request));
s_req->req = req;
s_req->start_ts = start_ts;
s_req->id = id;
s_req->async = 0;
s_req->index = -1;
TAILQ_INSERT_TAIL(&simplehttp_reqs, s_req, entries);
return s_req;
}
struct simplehttp_request *simplehttp_request_get(struct evhttp_request *req)
{
struct simplehttp_request *entry;
TAILQ_FOREACH(entry, &simplehttp_reqs, entries) {
if (req == entry->req) {
return entry;
}
}
return NULL;
}
uint64_t simplehttp_request_id(struct evhttp_request *req)
{
struct simplehttp_request *entry;
entry = simplehttp_request_get(req);
return entry ? entry->id : 0;
}
struct simplehttp_request *simplehttp_async_check(struct evhttp_request *req)
{
struct simplehttp_request *entry;
entry = simplehttp_request_get(req);
if (entry && entry->async) {
return entry;
}
return NULL;
}
void simplehttp_async_enable(struct evhttp_request *req)
{
struct simplehttp_request *entry;
if ((entry = simplehttp_request_get(req)) != NULL) {
entry->async = 1;
}
}
void simplehttp_request_finish(struct evhttp_request *req, struct simplehttp_request *s_req)
{
simplehttp_ts end_ts;
uint64_t req_time;
char id_buf[64];
simplehttp_ts_get(&end_ts);
req_time = simplehttp_ts_diff(s_req->start_ts, end_ts);
if (s_req->index != -1) {
simplehttp_stats_store(s_req->index, req_time);
}
if (verbose) {
sprintf(id_buf, "%"PRIu64, s_req->id);
simplehttp_log("", req, req_time, id_buf);
}
TAILQ_REMOVE(&simplehttp_reqs, s_req, entries);
free(s_req);
}
void simplehttp_async_finish(struct evhttp_request *req)
{
struct simplehttp_request *entry;
if ((entry = simplehttp_async_check(req))) {
simplehttp_request_finish(req, entry);
}
}
int get_argument_format(struct evkeyvalq *args)
{
int format_code = json_format;
char *format = (char *)evhttp_find_header(args, "format");
if (format && !strncmp(format, "txt", 3)) {
format_code = txt_format;
}
return format_code;
}
int get_int_argument(struct evkeyvalq *args, char *key, int default_value)
{
char *tmp;
if (!key) return default_value;
tmp = (char *)evhttp_find_header(args, (const char *)key);
if (tmp) {
return atoi(tmp);
}
return default_value;
}
double get_double_argument(struct evkeyvalq *args, char *key, double default_value)
{
char *tmp;
if (!key) return default_value;
tmp = (char *)evhttp_find_header(args, (const char *)key);
if (tmp) {
return atof(tmp);
}
return default_value;
}