-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUID_httpal.c
More file actions
154 lines (135 loc) · 4.27 KB
/
UID_httpal.c
File metadata and controls
154 lines (135 loc) · 4.27 KB
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
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2016-2018. Uniquid Inc. or its affiliates. All Rights Reserved.
*
* License is in the "LICENSE" file accompanying this file.
* See the License for the specific language governing permissions and limitations under the License.
*/
/*
* @file UID_httpal.c
*
* @date 09/dec/2017
* @author M. Palumbi
*/
/**
* @file UID_httpal.h
*
* http access abstraction layer.<br>
* Here is given an implementation using the curl library.
* The user of uidcore-c library must give its own implementation
* if lib curl is not available on the target system.
*/
#include <string.h>
#include <curl/curl.h>
#include "UID_httpal.h"
#include "UID_log.h"
typedef struct {
size_t size;
char *buffer;
} curl_context;
// callback from curl_easy_perform
static size_t curl_callback(char *buffer, size_t size, size_t nmemb, void *ctx)
{
size_t l = size*nmemb;
UID_log(UID_LOG_DEBUG,"httpget callback\n");
if (l < ((curl_context *)ctx)->size) {
memcpy(((curl_context *)ctx)->buffer, buffer, l);
((curl_context *)ctx)->buffer += l;
*((curl_context *)ctx)->buffer = 0;
((curl_context *)ctx)->size -= l;
return l;
}
else {
return -1;
}
}
/**
* Get data from url
*
* @param[in] curl pointer to an initialized UID_HttpOBJ struct
* @param[in] url url to contact
* @param[out] buffer pointer to buffer to be filled
* @param[in] size size of buffer
*
* @return UID_HTTP_OK no error
*/
int UID_httpget(UID_HttpOBJ *curl, char *url, char *buffer, size_t size)
{
curl_context ctx;
ctx.buffer = buffer;
ctx.size = size;
UID_log(UID_LOG_DEBUG,"enter httpget curl=%p\n", curl);
CURLcode retval = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 45L);
UID_log(UID_LOG_DEBUG,"setopt returns %d\n", retval);
retval = curl_easy_setopt(curl, CURLOPT_TIMEOUT, 45L);
UID_log(UID_LOG_DEBUG,"setopt returns %d\n", retval);
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ctx);
curl_easy_setopt(curl, CURLOPT_URL, url);
UID_log(UID_LOG_DEBUG,"calling easy_perform()\n");
retval = curl_easy_perform(curl);
UID_log(UID_LOG_DEBUG,"easy_perform() returns %d\n",retval);
return (retval == CURLE_OK ? UID_HTTP_OK : UID_HTTP_GET_ERROR);
}
typedef struct {
size_t buffer_size;
char *buffer;
} send_tx_context;
#ifdef UID_IMPLEMENTSENDTX
/**
* callback from curl_easy_perform
* returns the answer for the send from insight-api
*/
static size_t send_tx(char *buffer, size_t size, size_t nmemb, void *ctx)
{
size_t l = size*nmemb;
if (l < ((send_tx_context *)ctx)->buffer_size) {
memcpy(((send_tx_context *)ctx)->buffer, buffer, l);
((send_tx_context *)ctx)->buffer += l;
*((send_tx_context *)ctx)->buffer = 0;
((send_tx_context *)ctx)->buffer_size -= l;
return l;
}
else {
return -1;
}
}
/**
* Post data to url
* with the following format<br>
* Content-Type: application/x-www-form-urlencoded
*
* @param[in] curl pointer to an initialized UID_HttpOBJ struct
* @param[in] url url to contact
* @param[in] postdata data to be posted
* @param[out] ret pointer to the buffer to be filled
* @param[in] size size of buffer
*
* @return UID_HTTP_OK no error
*/
int UID_httppost(UID_HttpOBJ *curl, char *url, char *postdata, char *ret, size_t size)
{
send_tx_context ctx;
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, send_tx);
/* Set a pointer to our struct to pass to the callback */
ctx.buffer_size = size;
ctx.buffer = ret;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ctx);
curl_easy_setopt(curl, CURLOPT_URL, url);
/* setup post data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
/* perform the request */
return (CURLE_OK == curl_easy_perform(curl) ? UID_HTTP_OK : UID_HTTP_POST_ERROR);
}
#endif //UID_IMPLEMENTSENDTX
UID_HttpOBJ *UID_httpinit()
{
return curl_easy_init();
}
int UID_httpcleanup(UID_HttpOBJ *curl)
{
curl_easy_cleanup(curl);
return UID_HTTP_OK;
}