-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-client.h
131 lines (113 loc) · 3.47 KB
/
http-client.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
/*
* Copyright (C) 2021 "IoT.bzh"
* Author "Fulup Ar Foll" <[email protected]>
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
* $RP_END_LICENSE$
*
* Examples:
* GET httpSendGet(oidc->httpPool, "https://example.com", idp->headers, NULL|token, NULL|opts, callback, ctx);
* POST httpSendPost(oidc->httpPool, url, idp->headers, NULL|token, NULL|opts, (void*)post,datalen, callback, ctx);
*/
#pragma once
#include <curl/curl.h>
#include <sys/types.h>
#include <stdint.h>
#define MAGIC_HTTP_RQT 951357
#define MAGIC_HTTP_POOL 583498
#define DFLT_HEADER_MAX_LEN 1024
#define HTTP_DFLT_AGENT "afb-oidc-sgate/1.0"
typedef struct httpPoolS httpPoolT;
typedef enum
{
HTTP_HANDLE_FREE,
HTTP_HANDLE_KEEP,
} httpRqtActionT;
typedef struct
{
const char *tag;
const char *value;
} httpKeyValT;
typedef void (*httpFreeCtxCbT)(void *userData);
// curl options
typedef struct
{
const char *username;
const char *password;
const char *bearer;
const long timeout;
const long sslchk;
const long verbose;
const long maxsz;
const long speedlimit;
const long speedlow;
const long follow;
const long maxredir;
const char *proxy;
const char *cainfo;
const char *sslcert;
const char *sslkey;
const char *tostr;
const char *agent;
int ldap;
const httpKeyValT *headers;
const httpFreeCtxCbT freeCtx;
} httpOptsT;
typedef struct httpRqtS httpRqtT;
typedef httpRqtActionT (*httpRqtCbT)(httpRqtT *httpRqt);
// http request handle
typedef struct httpRqtS
{
int magic;
int verbose;
char *body;
char *headers;
char *ctype;
long length;
long hdrLen;
long bodyLen;
long status;
char error[CURL_ERROR_SIZE];
void *easy;
struct timespec startTime;
struct timespec stopTime;
uint64_t msTime;
void *userData;
httpRqtCbT callback;
httpFreeCtxCbT freeCtx;
} httpRqtT;
// mainloop glue API interface
typedef void *(*evtMainLoopCbT)();
typedef int (*multiTimerCbT)(httpPoolT *httpPool, long timeout);
typedef int (*multiSocketCbT)(httpPoolT *httpPool, CURL *easy, int sock, int action, void *sockp);
typedef int (*evtRunLoopCbT)(httpPoolT *httpPool, long seconds);
// glue callbacks handle
typedef struct
{
evtMainLoopCbT evtMainLoop;
evtRunLoopCbT evtRunLoop;
multiTimerCbT multiTimer;
multiSocketCbT multiSocket;
} httpCallbacksT;
// multi-pool handle
typedef struct httpPoolS
{
int magic;
int verbose;
CURLM *multi;
void *evtLoop;
void *evtTimer;
httpCallbacksT *callback;
} httpPoolT;
// glue proto to get mainloop callbacks
httpCallbacksT *glueGetCbs(void);
// API to build and lauch request (if httpPoolT==NULL then run synchronously)
int httpBuildQuery(const char *uid, char *response, size_t maxlen, const char *prefix, const char *url, httpKeyValT *query);
int httpSendPost(httpPoolT *pool, const char *url, const httpOptsT *opts, httpKeyValT *tokens, void *databuf, long datalen, httpRqtCbT callback, void *ctx);
int httpSendGet(httpPoolT *pool, const char *url, const httpOptsT *opts, httpKeyValT *tokens, httpRqtCbT callback, void *ctx);
// init curl multi pool with an abstract mainloop and corresponding callbacks
httpPoolT *httpCreatePool(void *evtLoop, httpCallbacksT *mainLoopCbs, int verbose);
// curl action callback to be called from glue layer
int httpOnSocketCB(httpPoolT *httpPool, int sock, int action);
int httpOnTimerCB(httpPoolT *httpPool);