Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/DefaultWebRequestCurl.cpp

Issue 10252013: Implemented curl support for web requests (Closed)
Patch Set: Addressed review comments Created April 12, 2013, 1:37 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <AdblockPlus/WebRequest.h>
2
3 AdblockPlus::WebRequest::~WebRequest()
4 {
5 }
6
7 #if defined(HAVE_CURL)
8
9 #include <sstream> 1 #include <sstream>
10 #include <cctype> 2 #include <cctype>
11 #include <algorithm> 3 #include <algorithm>
12 #include <curl/curl.h> 4 #include <curl/curl.h>
5 #include <AdblockPlus/WebRequest.h>
13 6
14 namespace 7 namespace
15 { 8 {
16 struct HeaderData 9 struct HeaderData
17 { 10 {
18 int status; 11 int status;
19 bool expectingStatus; 12 bool expectingStatus;
20 std::vector<std::string> headers; 13 std::vector<std::string> headers;
21 14
22 HeaderData() 15 HeaderData()
23 { 16 {
24 status = 0; 17 status = 0;
25 expectingStatus = true; 18 expectingStatus = true;
26 } 19 }
27 }; 20 };
28 21
29 unsigned int ConvertErrorCode(CURLcode code) 22 unsigned int ConvertErrorCode(CURLcode code)
30 { 23 {
31 switch (code) 24 switch (code)
32 { 25 {
33 case CURLE_OK: 26 case CURLE_OK:
34 return NS_OK; 27 return AdblockPlus::WebRequest::NS_OK;
35 case CURLE_FAILED_INIT: 28 case CURLE_FAILED_INIT:
36 return NS_ERROR_NOT_INITIALIZED; 29 return AdblockPlus::WebRequest::NS_ERROR_NOT_INITIALIZED;
37 case CURLE_UNSUPPORTED_PROTOCOL: 30 case CURLE_UNSUPPORTED_PROTOCOL:
38 return NS_ERROR_UNKNOWN_PROTOCOL; 31 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_PROTOCOL;
39 case CURLE_URL_MALFORMAT: 32 case CURLE_URL_MALFORMAT:
40 return NS_ERROR_MALFORMED_URI; 33 return AdblockPlus::WebRequest::NS_ERROR_MALFORMED_URI;
41 case CURLE_COULDNT_RESOLVE_PROXY: 34 case CURLE_COULDNT_RESOLVE_PROXY:
42 return NS_ERROR_UNKNOWN_PROXY_HOST; 35 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_PROXY_HOST;
43 case CURLE_COULDNT_RESOLVE_HOST: 36 case CURLE_COULDNT_RESOLVE_HOST:
44 return NS_ERROR_UNKNOWN_HOST; 37 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_HOST;
45 case CURLE_COULDNT_CONNECT: 38 case CURLE_COULDNT_CONNECT:
46 return NS_ERROR_CONNECTION_REFUSED; 39 return AdblockPlus::WebRequest::NS_ERROR_CONNECTION_REFUSED;
47 case CURLE_OUT_OF_MEMORY: 40 case CURLE_OUT_OF_MEMORY:
48 return NS_ERROR_OUT_OF_MEMORY; 41 return AdblockPlus::WebRequest::NS_ERROR_OUT_OF_MEMORY;
49 case CURLE_OPERATION_TIMEDOUT: 42 case CURLE_OPERATION_TIMEDOUT:
50 return NS_ERROR_NET_TIMEOUT; 43 return AdblockPlus::WebRequest::NS_ERROR_NET_TIMEOUT;
51 case CURLE_TOO_MANY_REDIRECTS: 44 case CURLE_TOO_MANY_REDIRECTS:
52 return NS_ERROR_REDIRECT_LOOP; 45 return AdblockPlus::WebRequest::NS_ERROR_REDIRECT_LOOP;
53 case CURLE_GOT_NOTHING: 46 case CURLE_GOT_NOTHING:
54 return NS_ERROR_NO_CONTENT; 47 return AdblockPlus::WebRequest::NS_ERROR_NO_CONTENT;
55 case CURLE_SEND_ERROR: 48 case CURLE_SEND_ERROR:
56 return NS_ERROR_NET_RESET; 49 return AdblockPlus::WebRequest::NS_ERROR_NET_RESET;
57 case CURLE_RECV_ERROR: 50 case CURLE_RECV_ERROR:
58 return NS_ERROR_NET_RESET; 51 return AdblockPlus::WebRequest::NS_ERROR_NET_RESET;
59 default: 52 default:
60 return NS_CUSTOM_ERROR_BASE + code; 53 return AdblockPlus::WebRequest::NS_CUSTOM_ERROR_BASE + code;
61 } 54 }
62 } 55 }
63 56
64 size_t ReceiveData(char* ptr, size_t size, size_t nmemb, void* userdata) 57 size_t ReceiveData(char* ptr, size_t size, size_t nmemb, void* userdata)
65 { 58 {
66 std::stringstream* stream = static_cast<std::stringstream*>(userdata); 59 std::stringstream* stream = static_cast<std::stringstream*>(userdata);
67 stream->write(ptr, size * nmemb); 60 stream->write(ptr, size * nmemb);
68 return nmemb; 61 return nmemb;
69 } 62 }
70 63
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 121 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
129 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ReceiveData); 122 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ReceiveData);
130 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseText); 123 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseText);
131 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ReceiveHeader); 124 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, ReceiveHeader);
132 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &headerData); 125 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &headerData);
133 126
134 struct curl_slist* headerList = 0; 127 struct curl_slist* headerList = 0;
135 for (HeaderList::const_iterator it = requestHeaders.begin(); 128 for (HeaderList::const_iterator it = requestHeaders.begin();
136 it != requestHeaders.end(); ++it) 129 it != requestHeaders.end(); ++it)
137 { 130 {
138 curl_slist_append(headerList, (it->first + ": " + it->second).c_str()); 131 headerList = curl_slist_append(headerList, (it->first + ": " + it->second) .c_str());
139 } 132 }
140 if (headerList) 133 if (headerList)
141 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList); 134 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList);
142 135
143 result.status = ConvertErrorCode(curl_easy_perform(curl)); 136 result.status = ConvertErrorCode(curl_easy_perform(curl));
144 result.responseStatus = headerData.status; 137 result.responseStatus = headerData.status;
145 result.responseText = responseText.str(); 138 result.responseText = responseText.str();
146 for (std::vector<std::string>::iterator it = headerData.headers.begin(); 139 for (std::vector<std::string>::iterator it = headerData.headers.begin();
147 it != headerData.headers.end(); ++it) 140 it != headerData.headers.end(); ++it)
148 { 141 {
(...skipping 22 matching lines...) Expand all
171 } 164 }
172 } 165 }
173 } 166 }
174 167
175 if (headerList) 168 if (headerList)
176 curl_slist_free_all(headerList); 169 curl_slist_free_all(headerList);
177 curl_easy_cleanup(curl); 170 curl_easy_cleanup(curl);
178 } 171 }
179 return result; 172 return result;
180 } 173 }
181 #else
182 AdblockPlus::ServerResponse AdblockPlus::DefaultWebRequest::GET(
183 const std::string& url, const HeaderList& requestHeaders) const
184 {
185 AdblockPlus::ServerResponse result;
186 result.status = NS_ERROR_FAILURE;
187 result.responseStatus = 0;
188 return result;
189 }
190 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld