-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.c
264 lines (214 loc) · 6.14 KB
/
http.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Duke 2016
Quick and Dirty HTTP POST and HTTP GET implementations!
*/
#include "parse.h"
#include "cpc.h"
#include "http.h"
#define BOUNDARY_ID "-------------123123123"
#ifndef __WIN32__
#define strnicmp strncasecmp
#endif
int httpConnect(char *host)
{
SOCKET sd;
struct sockaddr_in servaddr;
struct in_addr addr;
char **pptr;
int i;
struct hostent *hptr;
if ((hptr = gethostbyname(host)) == NULL)
{
return -1;
}
if (hptr->h_addrtype == AF_INET && (pptr = hptr->h_addr_list) != NULL)
{
sd = socket(AF_INET, SOCK_STREAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
i = 0;
while (hptr->h_addr_list[i] != 0)
addr.s_addr = *(u_long *) hptr->h_addr_list[i++];
servaddr.sin_addr = addr;
if ( connect(sd,(struct sockaddr *) &servaddr, sizeof(servaddr) ) >= 0 )
return sd;
}
return -1;
}
int httpClose(SOCKET sd)
{
if ( sd > 0 )
{
#ifdef __WIN32__
closesocket(sd);
#else
close(sd);
#endif
return 0;
}
return -1;
}
int httpSend(SOCKET sd, char *filename, unsigned char *data, int size, char *formname, char *path, char *host)
{
int i, chunkSize, contentLen;
char httpHeader[1024];
char httpContent[1024];
char httpEnd[256];
contentLen = sprintf(httpEnd, "\r\n\r\n--%s--", BOUNDARY_ID);
contentLen += sprintf(httpContent, "--%s\r\nContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n", BOUNDARY_ID, filename);
contentLen +=size;
// send HTTP POST header
i = sprintf(httpHeader, "POST %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\nContent-Type: multipart/form-data; boundary=%s\r\nContent-Length: %d\r\r\n\r\n", path, host, BOUNDARY_ID, contentLen);
send(sd, httpHeader, strlen(httpHeader), 0);
// send content
send(sd, httpContent, strlen(httpContent), 0);
// actual data
i = 0;
while ( i < size )
{
if ( (size-i) < 1460 )
chunkSize = size-i;
else
chunkSize = 1460;
send(sd, (char *)&data[i], chunkSize, 0);
i+=chunkSize;
}
// send boundary end
send(sd, httpEnd, strlen(httpEnd), 0);
return 0;
}
int httpSendRom(SOCKET sd, char *filename, unsigned char *data, int size, int slot, char *path, char *host, char *slotname)
{
int i, chunkSize, contentLen;
char httpHeader[1024];
char httpContent[2*1024];
char httpEnd[256];
contentLen = sprintf(httpEnd, "\r\n\r\n--%s--", BOUNDARY_ID);
contentLen += sprintf(httpContent, "--%s\r\nContent-Disposition: form-data; name=\"slotnum\"\r\n\r\n%i\r\n--%s\r\nContent-Disposition: form-data; name=\"slotname\"\r\n\r\n%s\r\n--%s\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n",
BOUNDARY_ID, slot, BOUNDARY_ID, slotname, BOUNDARY_ID, filename);
contentLen +=size;
// send HTTP POST header
i = sprintf(httpHeader, "POST %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\nContent-Type: multipart/form-data; boundary=%s\r\nContent-Length: %d\r\r\n\r\n", path, host, BOUNDARY_ID, contentLen);
send(sd, httpHeader, strlen(httpHeader), 0);
// send content
send(sd, httpContent, strlen(httpContent), 0);
// actual data
i = 0;
while ( i < size )
{
if ( (size-i) < 1460 )
chunkSize = size-i;
else
chunkSize = 1460;
send(sd, (char *)&data[i], chunkSize, 0);
i+=chunkSize;
}
// send boundary end
send(sd, httpEnd, strlen(httpEnd), 0);
return 0;
}
int getStringValue(char *string, char *buf, int size)
{ char len[16];
int i, j, k;
k = strlen(string);
i = 0;
while ( i < (size-k) )
{
if ( !strnicmp(&buf[i], string, k) )
{
i+=(k+2);
j = 0;
while ( i < size )
{
if ( (buf[i] >= '0') && (buf[i] <= '9') ) // skip leading spaces and other stuff
len[j++] = buf[i];
else
break;
i++;
}
len[j++] = 0;
return atoi(len);
}
i++;
}
return -1;
}
int httpResponse(SOCKET sd)
{
int n, i, ret;
char *response;
response = malloc(4096);
memset(response, 0, 4096);
i = 0;
while (i < 4096)
{
n = recv(sd, &response[i], 4096-i, 0 );
if ( n<= 0 )
break;
i+=n;
}
ret = getStringValue("HTTP/1.", response, i);
free(response);
return ret;
}
// send HTTP GET request and process response
int httpGet(SOCKET sd, char *host, char *url, int skipheader)
{
char httpReq[512];
char response[1460];
int i, n, pos;
memset(response, 0, sizeof(response));
i = sprintf(httpReq, "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: cpcxfer\r\n\r\n", url, host);
send(sd, httpReq, i, 0);
while (1)
{
n = recv(sd, &response[0], sizeof(response),0 );
if ( n <= 0 )
break;
// should do more buffer rotation, reloading and checking... but it's not needed as its always in one response - atleast here :P
pos = findString("200 OK", response, n);
if ( pos > 0 )
{ i = findString("Content-type: text/plain", &response[pos], n-pos);
if (i > 0 )
{
pos+=i;
// now find cr/lf/cr/lf and start saving...
i = findString("\r\n\r\n", &response[pos], n-pos);
if ( i > 0 )
{
int headerFound = 0;
FILE *f;
pos+=i;
// get filename from url
f = fopen(&url[pathPos(url, strlen(url))], "wb");
// rotate buffer if anything left
memcpy(&response[0], &response[pos], n-pos);
pos = n-pos;
while (1)
{
n = recv(sd, &response[pos], sizeof(response)-pos,0 );
n+=pos;
if ( n==0 )
break;
if ( (headerFound == 0) && skipheader )
{ // check if its a valid header at all (not going to remove it, if it doesnt have one!)
_cpchead *cpcheader = (_cpchead *)response;
if ( checksum16(response, 66) == cpcheader->checksum )
fwrite(&response[0x80], n-0x80, 1, f);
else
fwrite(&response[0], n, 1, f);
headerFound = 1;
}
else
fwrite(&response[0], n, 1, f);
pos = 0;
}
fclose(f);
return 0;
}
}
}
}
return -1;
}