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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
Http.cpp
part of the libcurl HTTP class written for C++
based partially on http://www.zedwood.com/article/125/cpp-libcurl-static-class, with many modifications made
*/
#include "Http.h"
using namespace std;
string Http::last_url;
string Http::current_url;
bool Http::use_proxy;
string Http::proxy_ip;
long Http::proxy_port;
long Http::remote_port;
string Http::get(const string &url){
return request(url, "", false, "");
}
string Http::get(const string &url, string referer){
return request(url, referer, false, "");
}
string Http::post(const string &url, map<string, string> params){
string postdata = build_param_string(params);
return request(url, "", true, postdata);
}
string Http::post(const string &url, map<string, string> params, string referer){
string postdata = build_param_string(params);
return request(url, referer, true, postdata);
}
string Http::get_current_url(){
return current_url;
}
string Http::get_last_url(){
return last_url;
}
void Http::set_proxy(string ip, long port){
use_proxy = true;
proxy_ip = ip;
proxy_port = port;
}
void Http::clear_proxy(){
use_proxy = false;
}
void Http::set_remote_port(long port){
remote_port = port;
}
string Http::request(const string &url, string referer, bool post, const string ¶ms){
static string buffer = "";
static char error_buffer[CURL_ERROR_SIZE];
error_buffer[0] = 0;
CURL *curl;
CURLcode result;
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Http::writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(buffer));
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ".cookies.cj");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, ".cookies.cj");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
if(post){
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
}
if(referer.length()){
curl_easy_setopt(curl, CURLOPT_REFERER, referer.c_str());
}
if(use_proxy){
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_ip.c_str());
if(proxy_port){
curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxy_port);
}
}
if(remote_port){
curl_easy_setopt(curl, CURLOPT_PORT, remote_port);
}
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(result == CURLE_OK){
return buffer;
}
else{
return "error";
}
}
return "error";
}
int Http::writer(char *data, size_t size, size_t nmemb, string *buffer){
int result = 0;
if(buffer != NULL){
buffer->append(data, size * nmemb);
result = size * nmemb;
}
return result;
}
string Http::urlencode(const string &str){
string encoded = "";
int max = str.length();
for(int i = 0; i < max; i++){
if(
(48 <= str[i] && str[i] <= 57) ||
(65 <= str[i] && str[i] <= 90) ||
(97 <= str[i] && str[i] <= 122) ||
(str[i] == '~' || str[i] == '!' || str[i] == '*' || str[i] == '(' || str[i] == ')' || str[i] == '\'')
){
encoded.append(&str[i], 1);
}
else{
encoded.append("%");
encoded.append(dechex(str[i]));
}
}
return encoded;
}
string Http::dechex(char dec){
char dig1 = (dec&0xF0)>>4;
char dig2 = (dec&0x0F);
if ( 0<= dig1 && dig1<= 9) dig1+=48; //0,48inascii
if (10<= dig1 && dig1<=15) dig1+=97-10; //a,97inascii
if ( 0<= dig2 && dig2<= 9) dig2+=48;
if (10<= dig2 && dig2<=15) dig2+=97-10;
string r;
r.append( &dig1, 1);
r.append( &dig2, 1);
return r;
}
string Http::build_param_string(map<string, string> params){
string param_string = "";
for(map<string, string>::iterator it = params.begin(); it != params.end(); it++){
param_string += it->first + "=" + urlencode(it->second) + "&";
}
param_string = param_string.substr(0, param_string.length() - 1);
return param_string;
}
|