-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
169 lines (150 loc) · 3.68 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <curl/curl.h>
#include <utility>
#include <string>
#include <map>
#include <boost/format.hpp>
#include <boost/regex.hpp>
#define RET_CURL(fun, ...) \
if((retCode = fun(__VA_ARGS__)) != CURLE_OK) \
{ \
printf("error[%s:%d]: %s\n", __FILE__, __LINE__, curl_easy_strerror(retCode)); \
return -1; \
}
CURL* pstCURLHandle = NULL;
bool bOutHeader = false;
bool bOutHttpStatus = false;
bool bNoOutContent = false;
const char* szUserAgent = NULL;
const char* szUrl = NULL;
std::string strHeader;
std::string strContent;
int iHttpStatus = 0;
size_t HttpWriteCallback(char* ptr, size_t size, size_t nmemb, void* userdata)
{
char* pbuffer = (char*)malloc(size * nmemb + 1);
memcpy(pbuffer, ptr, size * nmemb);
pbuffer[size * nmemb] = 0;
strContent.append(pbuffer);
free(pbuffer);
return size * nmemb;
}
size_t HttpHeaderCallback(char* ptr, size_t size, size_t nmemb, void* userdata)
{
char* pbuffer = (char*)malloc(size * nmemb + 1);
memcpy(pbuffer, ptr, size * nmemb);
pbuffer[size * nmemb] = 0;
strHeader.append(pbuffer);
free(pbuffer);
return size * nmemb;
}
void print_usage()
{
printf("usage: download [-help] [-status] [-nocontent] [-head] [-useragent \"user agent\"] [url]\n");
}
int main(int argc, char* argv[])
{
for(int i=1; i<argc; ++i)
{
if(strcmp(argv[i], "-help") == 0)
{
print_usage();
return -1;
}
else if(strcmp(argv[i], "-nocontent") == 0)
{
bNoOutContent = true;
}
else if(strcmp(argv[i], "-status") == 0)
{
bOutHttpStatus = true;
}
else if(strcmp(argv[i], "-head") == 0)
{
bOutHeader = true;
}
else if(strcmp(argv[i], "-useragent") == 0)
{
++i;
if(i < argc)
szUserAgent = argv[i];
else
{
print_usage();
return -1;
}
}
else
{
szUrl = argv[i];
}
}
CURLcode retCode = CURLE_OK;
RET_CURL(curl_global_init, CURL_GLOBAL_ALL);
pstCURLHandle = curl_easy_init();
if(pstCURLHandle == NULL)
{
printf("error[%s:%d]: create CURL handle fail.\n", __FILE__, __LINE__);
return -1;
}
RET_CURL(curl_easy_setopt, pstCURLHandle, CURLOPT_HEADER, 0);
RET_CURL(curl_easy_setopt, pstCURLHandle, CURLOPT_WRITEFUNCTION, HttpWriteCallback);
RET_CURL(curl_easy_setopt, pstCURLHandle, CURLOPT_HEADERFUNCTION, HttpHeaderCallback);
if(szUserAgent != NULL)
RET_CURL(curl_easy_setopt, pstCURLHandle, CURLOPT_USERAGENT, szUserAgent);
if(szUrl == NULL)
{
char buffer[4097];
int size = 0;
int len = 4096;
int offset = 0;
memset(buffer, 0, 4097);
while(-1 != (size = read(STDIN_FILENO, buffer + offset, len)))
{
if(size == 0)
break;
char* pStart = buffer;
char* pEnd = NULL;
while((pEnd = strchr(pStart, '\n')) != NULL)
{
pEnd[0] = 0x0;
RET_CURL(curl_easy_setopt, pstCURLHandle, CURLOPT_URL, pStart);
RET_CURL(curl_easy_perform, pstCURLHandle);
pStart = pEnd + 1;
}
strcpy(buffer, pStart);
offset = strlen(pStart);
len = 4096 - offset;
memset(buffer + offset, 0, len);
}
}
else
{
RET_CURL(curl_easy_setopt, pstCURLHandle, CURLOPT_URL, szUrl);
RET_CURL(curl_easy_perform, pstCURLHandle);
if(bOutHttpStatus)
{
boost::regex rgxHttpStatus("^HTTP/([^ ]+) ([0-9]+)");
boost::smatch what;
if(boost::regex_search(strHeader, what, rgxHttpStatus))
printf("%d\n", atoi(what[2].str().c_str()));
else
printf("500\n");
}
if(bOutHeader)
printf("%s", strHeader.c_str());
if(!bNoOutContent)
printf("%s", strContent.c_str());
}
curl_easy_cleanup(pstCURLHandle);
curl_global_cleanup();
return 0;
}