Orca
A bot framework that is easy to reason about, easy to debug, and easy to use.
user-agent.h
1 #ifndef USER_AGENT_H
2 #define USER_AGENT_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif // __cplusplus
7 
8 #include <stdint.h> /* uint64_t */
9 #include <curl/curl.h>
10 #include "ntl.h" /* struct sized_buffer */
11 #include "logconf.h"
12 
13 /* FORWARD DECLARATIONS */
14 struct user_agent; // the user agent that perform requests
15 
16 //possible http methods
17 enum http_method {
18  HTTP_INVALID = -1,
19  HTTP_DELETE,
20  HTTP_GET,
21  HTTP_POST,
22  HTTP_MIMEPOST,
23  HTTP_PATCH,
24  HTTP_PUT
25 };
26 
27 /* COMMON HTTP RESPONSE CODES
28 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes */
29 #define HTTP_OK 200
30 #define HTTP_CREATED 201
31 #define HTTP_NO_CONTENT 204
32 #define HTTP_NOT_MODIFIED 304
33 #define HTTP_BAD_REQUEST 400
34 #define HTTP_UNAUTHORIZED 401
35 #define HTTP_FORBIDDEN 403
36 #define HTTP_NOT_FOUND 404
37 #define HTTP_METHOD_NOT_ALLOWED 405
38 #define HTTP_UNPROCESSABLE_ENTITY 422
39 #define HTTP_TOO_MANY_REQUESTS 429
40 #define HTTP_GATEWAY_UNAVAILABLE 502
41 
42 #define UA_MAX_HEADER_SIZE 100 + 1
43 #define UA_MAX_URL_LEN 512 + 1
44 
45 //callback for object to be loaded by api response
46 typedef void (load_obj_cb)(char *str, size_t len, void *p_obj);
47 typedef void (cxt_load_obj_cb)(void * cxt, char *str, size_t len, void *p_obj);
48 
50  void *cxt; // the context for cxt_ok_cb;
51 
52  load_obj_cb *ok_cb;
53  void *ok_obj; // the pointer to be passed to ok_cb
54 
55  load_obj_cb *err_cb;
56  void *err_obj; // the pointer to be passed to err_cb
57 
58  cxt_load_obj_cb *cxt_ok_cb; // ok call back with an execution context
59  cxt_load_obj_cb *cxt_err_cb; // err call back with an execution context
60 };
61 
66  char *buf;
67  size_t length;
71  size_t bufsize;
72 
77  struct {
78  struct {
79  uintptr_t idx;
80  size_t size;
81  } field, value;
82  } pairs[UA_MAX_HEADER_SIZE];
86  int size;
87 };
88 
89 struct ua_resp_body {
93  char *buf;
94  size_t length;
98  size_t bufsize;
99 };
100 
101 struct ua_info {
108  ORCAcode code;
112  char req_url[UA_MAX_URL_LEN];
116  uint64_t req_tstamp;
125 };
126 
127 char* http_code_print(int httpcode);
128 char* http_reason_print(int httpcode);
129 char* http_method_print(enum http_method method);
130 enum http_method http_method_eval(char method[]);
131 
132 void ua_reqheader_add(struct user_agent *ua, char field[], char value[]);
133 void ua_reqheader_del(struct user_agent *ua, char field[]);
134 char* ua_reqheader_str(struct user_agent *ua, char *buf, size_t bufsize);
135 
136 void ua_easy_setopt(struct user_agent *ua, void *data, void (setopt_cb)(CURL *ehandle, void *data));
137 void ua_mime_setopt(struct user_agent *ua, void *data, curl_mime* (mime_cb)(CURL *ehandle, void *data)); // @todo this is temporary
138 
139 struct user_agent* ua_init(struct logconf *conf);
140 void ua_cleanup(struct user_agent *ua);
141 
142 void ua_set_url(struct user_agent *ua, const char base_url[]);
143 char* ua_get_url(struct user_agent *ua);
144 void ua_block_ms(struct user_agent *ua, const uint64_t wait_ms);
145 ORCAcode ua_vrun(
146  struct user_agent *ua,
147  struct ua_info *info,
148  struct ua_resp_handle *resp_handle,
149  struct sized_buffer *req_body,
150  enum http_method http_method, char endpoint[], va_list args);
151 ORCAcode ua_run(
152  struct user_agent *ua,
153  struct ua_info *info,
154  struct ua_resp_handle *resp_handle,
155  struct sized_buffer *req_body,
156  enum http_method http_method, char endpoint[], ...);
157 
158 void ua_info_cleanup(struct ua_info *info);
159 struct sized_buffer ua_info_respheader_field(struct ua_info *info, char field[]);
160 struct sized_buffer ua_info_get_resp_body(struct ua_info *info);
161 
162 #ifdef __cplusplus
163 }
164 #endif // __cplusplus
165 
166 #endif // USER_AGENT_H
ua_info::req_url
char req_url[UA_MAX_URL_LEN]
Definition: user-agent.h:112
ua_resp_body
Definition: user-agent.h:89
ua_resp_handle
Definition: user-agent.h:49
ua_info
Definition: user-agent.h:101
ua_resp_header
Definition: user-agent.h:62
ua_info::resp_body
struct ua_resp_body resp_body
Definition: user-agent.h:124
ua_info::code
ORCAcode code
Definition: user-agent.h:108
ua_info::resp_header
struct ua_resp_header resp_header
Definition: user-agent.h:120
ua_resp_header::buf
char * buf
Definition: user-agent.h:66
ua_resp_body::buf
char * buf
Definition: user-agent.h:93
ua_resp_header::size
int size
Definition: user-agent.h:86
ua_resp_header::bufsize
size_t bufsize
Definition: user-agent.h:71
ua_resp_body::bufsize
size_t bufsize
Definition: user-agent.h:98
ua_resp_header::pairs
struct ua_resp_header::@12 pairs[UA_MAX_HEADER_SIZE]
ua_info::req_tstamp
uint64_t req_tstamp
Definition: user-agent.h:116