-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_http_request.h
130 lines (116 loc) · 3.91 KB
/
async_http_request.h
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
#ifndef _ASYNC_HTTP_REQUEST_H
#define _ASYNC_HTTP_REQUEST_H
struct async_http_attr;
typedef struct async_http_attr async_http_attr_t;
struct async_http_request;
typedef struct async_http_request async_http_request_t;
/**
* An enumeration that indicates current state of async_http_request_t.
*
* It can be obtained via async_http_request_run, async_http_request_wait,
* and async_http_getstate.
*/
enum async_http_request_state {
ASYNC_HTTP_REQUEST_DONE = 0,
ASYNC_HTTP_REQUEST_RUNNING = 1,
ASYNC_HTTP_REQUEST_FAILED = 2,
ASYNC_HTTP_REQUEST_NOT_STARTED = 3
};
typedef enum async_http_request_state async_http_request_state_t;
/**
* Create and init async_http_request_t object.
*
* This function utilizes 'attrs', but does not stores any reference to it
* - async_http_attr_destroy can be called just after async_http_request_init
* and nothing wrong will happen.
*
* Created object should be cleaned up via async_http_request_destroy.
*
* @param attr attributes like HTTP URL or HTTP_PROXY
* @return a new async_http_request_t object.
*/
async_http_request_t *async_http_request_init(async_http_attr_t *attr);
/**
* Start async_http_request_t.
*
* This function starts fresh http request in an async/non-blocking manner
* (but it does not do anything if request is already running).
*
* It can be called at the beginning of the async_http_request_t object usage
* or to reuse it.
*
* Note that it can fail immediately if an object was initialized with a faulty
* 'attrs'.
*
* @param request initialized async_http_request_t object
* @return an enumeration containing request state
*/
async_http_request_state_t async_http_request_start(async_http_request_t *request);
/**
* Stop running async_http_request_t.
*
* This function does not have any effect if async_http_request_t is not running.
*
* @param request initialized async_http_request_t object
*/
void async_http_request_stop(async_http_request_t *request);
/**
* Try to finalize async_http_requst_t in a given 'timeout_ms'.
*
* This function does not have any effect if async_http_request_t is not running.
*
* @param request initialized async_http_request_t object
* @return an enumeration containing request state
*/
async_http_request_state_t async_http_request_wait(async_http_request_t *request, int timeout_ms);
/**
* Get a response from the server.
*
* It returns empty string ("") by default, so check request state before proceeding.
*
* @param request initialized async_http_request_t object
* @return HTTP text response
*/
const char * async_http_request_getresponse(const async_http_request_t *request);
/**
* Get an HTTP request response code.
*
* It returns 0 by default, so check request state before proceeding.
*
* @param request initialized async_http_request_t object
* @return HTTP response code
*/
long async_http_request_getresponsecode(const async_http_request_t *request);
/**
* Get a time that took to finalize the request.
*
* @param request initialized async_http_request_t object
* @return total time
*/
double async_http_request_gettotaltime(const async_http_request_t *request);
/**
* Get last known state of async_http_request_t.
*
* @param request async_http_request_t object
* @return an enumeration containing request state
*/
async_http_request_state_t async_http_request_getstate(const async_http_request_t *request);
/**
* Get last connected host ip.
*
* @param request async_http_request_t object
* @return an IP of last connected host
*/
const char *async_http_request_gethostip(const async_http_request_t *request);
/**
* Destroy async_http_request_t object.
*
* This function cleans up internal structures of async_http_request_t and cancels
* a running connection if there is any.
*
* Note that passed object should not be used anymore after calling *_destroy.
*
* @param request async_http_request_t object
*/
void async_http_request_destroy(async_http_request_t *request);
#endif // _ASYNC_HTTP_REQUEST_H