-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffer_curl.cpp
118 lines (98 loc) · 3.3 KB
/
buffer_curl.cpp
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
/*
* UniversalContainer library.
* Copyright Jason Denton, 2008,2010.
* Made available under the new BSD license, as described in LICENSE
*
* Send comments and bug reports to [email protected]
* http://www.greatpanic.com/code.html
*/
/* Routines to use Buffers with curl. */
#include <curl/curl.h>
#include "buffer.h"
namespace JAD {
//CURL receive call back
size_t curl_receive_data(void* data, size_t size, size_t nmemb, void* b)
{
Buffer* buffer = static_cast<Buffer*>(b);
char* tmp = (char*) data;
size_t sent = size * nmemb;
bool result = buffer->put_data(tmp,sent);
if (result) return sent;
else return 0;
}
//CURL send call back
size_t curl_send_data(void* data, size_t size, size_t nmemb, void* b)
{
size_t want = size * nmemb;
Buffer* buffer = static_cast<Buffer*>(b);
char* tmp = (char*) data;
return buffer->copy_out(tmp,want);
}
//Wrapper to setup a curl handle with appropriate send and
//receive.
CURL* setup_curl_handle(const char* url, Buffer* send, Buffer* receive)
{
CURL* curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_NOPROGRESS,1);
curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1);
curl_easy_setopt(curl,CURLOPT_URL,url);
if (send) {
curl_easy_setopt(curl,CURLOPT_READFUNCTION, curl_send_data );
curl_easy_setopt(curl,CURLOPT_READDATA, send);
}
if (receive) {
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, curl_receive_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA, receive);
}
return curl;
}
//post a buffer, and get a buffer in response. Also get the mime-type
//of the results.
Buffer* http_post_buffer(const char* url, Buffer* send,
string& return_type, const char* send_type, const int timeout)
{
Buffer* receive = new Buffer;
CURL* curl;
char curl_error[CURL_ERROR_SIZE];
size_t send_len = send->length - send->rpos;
struct curl_slist* hlist=NULL;
curl = setup_curl_handle(url,send,receive);
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,curl_error);
curl_easy_setopt(curl,CURLOPT_POST,1);
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,send_len);
if (timeout > -1) curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,(long)timeout);
if (!send_type) send_type = "application/x-www-form-urlencoded";
hlist = curl_slist_append(NULL, send_type);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
int rc = curl_easy_perform(curl);
if (rc) {
delete receive;
return NULL;
}
char* rt = NULL;
curl_easy_getinfo(curl,CURLINFO_CONTENT_TYPE,&rt);
return_type = strtok(rt,";");
curl_easy_cleanup(curl);
return receive;
}
//get a web page into a buffer
Buffer* http_get_buffer(const char* url, string& return_type, const int timeout)
{
Buffer* receive = new Buffer;
CURL* curl;
char curl_error[CURL_ERROR_SIZE];
curl = setup_curl_handle(url,NULL,receive);
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,curl_error);
if (timeout > -1) curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,(long)timeout);
int rc = curl_easy_perform(curl);
if (rc) {
delete receive;
return NULL;
}
char* rt = NULL;
curl_easy_getinfo(curl,CURLINFO_CONTENT_TYPE,&rt);
return_type = strtok(rt,";");
curl_easy_cleanup(curl);
return receive;
}
} //end namespace