forked from OpenIPC/uget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uget.c
170 lines (143 loc) · 3.38 KB
/
uget.c
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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define ERR_GENERAL 1
#define ERR_SOCKET 2
#define ERR_GETADDRINFO 3
#define ERR_CONNECT 4
#define ERR_SEND 5
#define ERR_USAGE 6
#define NDEBUG
int get_http_respcode(const char *inpbuf) {
char proto[4096], descr[4096];
int code;
if (sscanf(inpbuf, "%s %d %s", proto, &code, descr) < 2)
return -1;
return code;
}
int download(int writefd, char *hostname, char *uri) {
int ret = ERR_GENERAL;
struct addrinfo hints, *res, *res0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int err = getaddrinfo(hostname, "80", &hints, &res0);
if (err) {
#ifndef NDEBUG
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
#endif
return ERR_GETADDRINFO;
}
int s = -1;
for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
ret = ERR_SOCKET;
continue;
}
#ifndef NDEBUG
char buf[256];
inet_ntop(res->ai_family, &((struct sockaddr_in *)res->ai_addr)->sin_addr,
buf, sizeof(buf));
fprintf(stderr, "Connecting to %s...\n", buf);
#endif
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
ret = ERR_CONNECT;
close(s);
s = -1;
continue;
}
break; /* okay we got one */
}
freeaddrinfo(res0);
if (s < 0) {
return ret;
}
char buf[4096];
// use the hack to save some space in .rodata
strcpy(buf, "GET /");
if (uri) {
strncat(buf, uri, sizeof(buf) - strlen(buf) - 1);
}
strncat(buf, " HTTP/1.0\r\nHost: ", sizeof(buf) - strlen(buf) - 1);
strncat(buf, hostname, sizeof(buf) - strlen(buf) - 1);
strncat(buf, "\r\n\r\n", sizeof(buf) - strlen(buf) - 1);
int tosent = strlen(buf);
int nsent = send(s, buf, tosent, 0);
if (nsent != tosent)
return ERR_SEND;
int header = 1;
int nrecvd;
while ((nrecvd = recv(s, buf, sizeof(buf), 0))) {
char *ptr = buf;
if (header) {
ptr = strstr(buf, "\r\n\r\n");
if (!ptr)
continue;
int rcode = get_http_respcode(buf);
if (rcode / 100 != 2)
return rcode / 100 * 10 + rcode % 10;
header = 0;
ptr += 4;
nrecvd -= ptr - buf;
}
write(writefd, ptr, nrecvd);
}
return 0;
}
int main(int argc, char **argv) {
if (argc < 2)
return ERR_USAGE;
int url_arg = 1;
int run_program = 0;
if (argc == 3) {
if (strcmp("run", argv[1]) != 0)
return ERR_USAGE;
run_program = 1;
url_arg++;
}
char *hostname = argv[url_arg];
char *uri = NULL;
char *url = hostname;
while (*url) {
if (*url == '/') {
*url++ = 0;
uri = url;
break;
}
url++;
}
int fd = STDOUT_FILENO;
char temfname[] = "/tmp/ugetXXXXXX";
if (run_program) {
fd = mkstemp(temfname);
}
int ret = download(fd, hostname, uri);
if (ret)
goto cleanup;
if (run_program) {
fchmod(fd, S_IRUSR | S_IXUSR);
close(fd);
int child = fork();
if (child) {
int wstatus;
wait(&wstatus);
ret = WEXITSTATUS(wstatus);
} else {
execlp(temfname, temfname, (char *)NULL);
return EXIT_FAILURE;
}
}
cleanup:
if (run_program)
unlink(temfname);
return ret;
}