-
Notifications
You must be signed in to change notification settings - Fork 13
/
naett.h
145 lines (122 loc) · 4.28 KB
/
naett.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef LIBNAETT_H
#define LIBNAETT_H
#ifdef __cplusplus
extern "C" {
#endif
#if __ANDROID__
#include <jni.h>
typedef JavaVM* naettInitData;
#else
typedef void* naettInitData;
#endif
#define NAETT_UA "Naett/1.0"
/**
* @brief Global init method.
* Call to initialize the library.
*/
void naettInit(naettInitData initThing);
typedef struct naettReq naettReq;
typedef struct naettRes naettRes;
// If naettReadFunc is called with NULL dest, it must respond with the body size
typedef int (*naettReadFunc)(void* dest, int bufferSize, void* userData);
typedef int (*naettWriteFunc)(const void* source, int bytes, void* userData);
typedef int (*naettHeaderLister)(const char* name, const char* value, void* userData);
// Option to `naettRequest`
typedef struct naettOption naettOption;
// Sets request method. Defaults to "GET".
naettOption* naettMethod(const char* method);
// Adds a request header.
naettOption* naettHeader(const char* name, const char* value);
// Sets the request body. Ignored if a body reader is configured.
// The body is not copied, and the passed pointer must be valid for the
// lifetime of the request.
naettOption* naettBody(const char* body, int size);
// Sets a request body reader.
naettOption* naettBodyReader(naettReadFunc reader, void* userData);
// Sets a response body writer.
naettOption* naettBodyWriter(naettWriteFunc writer, void* userData);
// Sets connection timeout in milliseconds.
naettOption* naettTimeout(int milliSeconds);
// Sets the user agent.
naettOption* naettUserAgent(const char *userAgent);
/**
* @brief Creates a new request to the specified url.
* Use varargs options to configure the connection and following request.
*/
#define naettRequest(url, ...) naettRequest_va(url, countOptions(__VA_ARGS__), ##__VA_ARGS__)
/**
* @brief Creates a new request to the specified url.
* Uses an array of options rather than varargs.
*/
naettReq* naettRequestWithOptions(const char* url, int numOptions, const naettOption** options);
/**
* @brief Makes a request and returns a response object.
* The actual request is processed asynchronously, use `naettComplete`
* to check if the response is completed.
*
* A request object can be reused multiple times to make requests, but
* there can be only one active request using the same request object.
*/
naettRes* naettMake(naettReq* request);
/**
* @brief Frees a previously allocated request object.
* The request must not have any pending responses.
*/
void naettFree(naettReq* request);
/**
* @brief Checks if a response is complete, with a result
* or with an error.
* Use `naettGetStatus` to get the status.
*/
int naettComplete(const naettRes* response);
enum naettStatus {
naettConnectionError = -1,
naettProtocolError = -2,
naettReadError = -3,
naettWriteError = -4,
naettGenericError = -5,
naettProcessing = 0,
};
/**
* @brief Returns the status of a response.
* Status codes > 0 are HTTP status codes as returned by the server.
* Status codes < 0 are processing errors according to the `naettStatus`
* enum values.
*/
int naettGetStatus(const naettRes* response);
/**
* @brief Returns the response body.
* The body returned by this method is always empty when a custom
* body reader has been set up using the `naettBodyReader` option.
*/
const void* naettGetBody(naettRes* response, int* outSize);
/**
* @brief Returns the HTTP header value for the specified header name.
*/
const char* naettGetHeader(naettRes* response, const char* name);
/**
* @brief Returns how many bytes have been read from the response so far,
* and the integer pointed to by totalSize gets the Content-Length if available,
* or -1 if not (or 0 if headers have not been read yet).
*/
int naettGetTotalBytesRead(naettRes* response, int* totalSize);
/**
* @brief Enumerates all response headers as long as the `lister`
* returns true.
*/
void naettListHeaders(naettRes* response, naettHeaderLister lister, void* userData);
/**
* @brief Returns the request that initiated this response
*/
naettReq* naettGetRequest(naettRes* response);
/**
* @brief Closes a response object.
*/
void naettClose(naettRes* response);
// Varargs glue
naettReq* naettRequest_va(const char* url, int numOptions, ...);
#define countOptions(...) ((sizeof((void*[]){ __VA_ARGS__ }) / sizeof(void*)))
#ifdef __cplusplus
}
#endif
#endif // LIBNAETT_H