-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.c
699 lines (573 loc) · 16.4 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
* http client for RT-Thread
*/
#include "http.h"
#include <ctype.h>
#include <dfs_posix.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#define HTTP_RCV_TIMEO 6000 /* 6 second */
const char _http_get[] = "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: RT-Thread HTTP Agent\r\nConnection: Keep-Alive\r\nCookie: name=\"RT-Thread\"; ac=\"1281620086\"\r\n\r\n";
const char _shoutcast_get[] = "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: RT-Thread HTTP Agent\r\nIcy-MetaData: 1\r\nConnection: close\r\n\r\n";
extern long int strtol(const char *nptr, char **endptr, int base);
//
// This function will parse the Content-Length header line and return the file size
//
int http_parse_content_length(char *mime_buf)
{
char *line;
line = strstr(mime_buf, "CONTENT-LENGTH:");
line += strlen("CONTENT-LENGTH:");
// Advance past any whitepace characters
while((*line == ' ') || (*line == '\t')) line++;
return (int)strtol(line, RT_NULL, 10);
}
//
// This function will parse the initial response header line and return 0 for a "200 OK",
// or return the error code in the event of an error (such as 404 - not found)
//
int http_is_error_header(char *mime_buf)
{
char *line;
int i;
int code;
line = strstr(mime_buf, "HTTP/1.");
line += strlen("HTTP/1.");
// Advance past minor protocol version number
line++;
// Advance past any whitespace characters
while((*line == ' ') || (*line == '\t')) line++;
// Terminate string after status code
for(i = 0; ((line[i] != ' ') && (line[i] != '\t')); i++);
line[i] = '\0';
code = (int)strtol(line, RT_NULL, 10);
if( code == 200 )
return 0;
else
return code;
}
int shoutcast_is_error_header(char *mime_buf)
{
char *line;
int i;
int code;
line = strstr(mime_buf, "ICY");
line += strlen("ICY");
// Advance past minor protocol version number
line++;
// Advance past any whitespace characters
while((*line == ' ') || (*line == '\t')) line++;
// Terminate string after status code
for(i = 0; ((line[i] != ' ') && (line[i] != '\t')); i++);
line[i] = '\0';
code = (int)strtol(line, RT_NULL, 10);
if( code == 200 )
return 0;
else
return code;
}
//
// When a request has been sent, we can expect mime headers to be
// before the data. We need to read exactly to the end of the headers
// and no more data. This readline reads a single char at a time.
//
int http_read_line( int socket, char * buffer, int size )
{
char * ptr = buffer;
int count = 0;
int rc;
// Keep reading until we fill the buffer.
while ( count < size )
{
rc = recv( socket, ptr, 1, 0 );
if ( rc <= 0 ) return rc;
if ((*ptr == '\n'))
{
ptr ++;
count++;
break;
}
// increment after check for cr. Don't want to count the cr.
count++;
ptr++;
}
// Terminate string
*ptr = '\0';
// return how many bytes read.
return count;
}
/*
* resolve server address
* @param server the server sockaddress
* @param url the input URL address, for example, http://www.rt-thread.org/index.html
* @param host_addr the buffer pointer to save server host address
* @param request the pointer to point the request url, for example, /index.html
*
* @return 0 on resolve server address OK, others failed
*/
int http_resolve_address(struct sockaddr_in *server, const char * url, char *host_addr, char** request)
{
char *ptr;
char port[6] = "80"; /* default port of 80(HTTP) */
int i = 0, is_domain;
struct hostent *hptr;
/* strip http: */
ptr = strchr(url, ':');
if (ptr != NULL)
{
url = ptr + 1;
}
/* URL must start with double forward slashes. */
if((url[0] != '/') || (url[1] != '/' )) return -1;
url += 2; is_domain = 0;
i = 0;
/* allow specification of port in URL like http://www.server.net:8080/ */
while (*url)
{
if (*url == '/') break;
if (*url == ':')
{
unsigned char w;
for (w = 0; w < 5 && url[w + 1] != '/' && url[w + 1] != '\0'; w ++)
port[w] = url[w + 1];
/* get port ok */
port[w] = '\0';
url += w + 1;
break;
}
if ((*url < '0' || *url > '9') && *url != '.')
is_domain = 1;
host_addr[i++] = *url;
url ++;
}
*request = (char*)url;
/* get host addr ok. */
host_addr[i] = '\0';
if (is_domain)
{
/* resolve the host name. */
hptr = gethostbyname(host_addr);
if(hptr == 0)
{
rt_kprintf("HTTP: failed to resolve domain '%s'\n", host_addr);
return -1;
}
memcpy(&server->sin_addr, *hptr->h_addr_list, sizeof(server->sin_addr));
}
else
{
inet_aton(host_addr, (struct in_addr*)&(server->sin_addr));
}
/* set the port */
server->sin_port = htons((int) strtol(port, NULL, 10));
server->sin_family = AF_INET;
return 0;
}
//
// This is the main HTTP client connect work. Makes the connection
// and handles the protocol and reads the return headers. Needs
// to leave the stream at the start of the real data.
//
static int http_connect(struct http_session* session,
struct sockaddr_in * server, char *host_addr, const char *url)
{
int socket_handle;
int peer_handle;
int rc;
char mimeBuffer[100];
int timeout = HTTP_RCV_TIMEO;
if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
{
rt_kprintf( "HTTP: SOCKET FAILED\n" );
return -1;
}
/* set recv timeout option */
setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void*)&timeout, sizeof(timeout));
peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
if ( peer_handle < 0 )
{
rt_kprintf( "HTTP: CONNECT FAILED %i\n", peer_handle );
return -1;
}
{
char *buf;
rt_uint32_t length;
buf = rt_malloc (512);
if (*url)
length = rt_snprintf(buf, 512, _http_get, url, host_addr);
else
length = rt_snprintf(buf, 512, _http_get, "/", host_addr);
rc = send(peer_handle, buf, length, 0);
// rt_kprintf("HTTP request:\n%s", buf);
/* release buffer */
rt_free(buf);
}
// We now need to read the header information
while ( 1 )
{
int i;
// read a line from the header information.
rc = http_read_line( peer_handle, mimeBuffer, 100 );
// rt_kprintf(">> %s\n", mimeBuffer);
if ( rc < 0 ) return rc;
// End of headers is a blank line. exit.
if (rc == 0) break;
if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
// Convert mimeBuffer to upper case, so we can do string comps
for(i = 0; i < strlen(mimeBuffer); i++)
mimeBuffer[i] = toupper(mimeBuffer[i]);
if(strstr(mimeBuffer, "HTTP/1.")) // First line of header, contains status code. Check for an error code
{
rc = http_is_error_header(mimeBuffer);
if(rc)
{
rt_kprintf("HTTP: status code = %d!\n", rc);
return -rc;
}
}
if(strstr(mimeBuffer, "CONTENT-LENGTH:"))
{
session->size = http_parse_content_length(mimeBuffer);
rt_kprintf("size = %d\n", session->size);
}
}
// We've sent the request, and read the headers. SockHandle is
// now at the start of the main data read for a file io read.
return peer_handle;
}
struct http_session* http_session_open(const char* url)
{
int peer_handle = 0;
struct sockaddr_in server;
char *request, host_addr[32];
struct http_session* session;
session = (struct http_session*) rt_malloc(sizeof(struct http_session));
if(session == RT_NULL) return RT_NULL;
session->size = 0;
session->position = 0;
/* Check valid IP address and URL */
if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
{
rt_free(session);
return RT_NULL;
}
// Now we connect and initiate the transfer by sending a
// request header to the server, and receiving the response header
if((peer_handle = http_connect(session, &server, host_addr, request)) < 0)
{
rt_kprintf("HTTP: failed to connect to '%s'!\n", host_addr);
rt_free(session);
return RT_NULL;
}
// http connect returns valid socket. Save in handle list.
session->socket = peer_handle;
/* open successfully */
return session;
}
rt_size_t http_session_read(struct http_session* session, rt_uint8_t *buffer, rt_size_t length)
{
int bytesRead = 0;
int totalRead = 0;
int left = length;
// Read until: there is an error, we've read "size" bytes or the remote
// side has closed the connection.
do
{
bytesRead = recv(session->socket, buffer + totalRead, left, 0);
if(bytesRead <= 0) break;
left -= bytesRead;
totalRead += bytesRead;
} while(left);
return totalRead;
}
rt_off_t http_session_seek(struct http_session* session, rt_off_t offset, int mode)
{
switch(mode)
{
case SEEK_SET:
session->position = offset;
break;
case SEEK_CUR:
session->position += offset;
break;
case SEEK_END:
session->position = session->size + offset;
break;
}
return session->position;
}
int http_session_close(struct http_session* session)
{
lwip_close(session->socket);
rt_free(session);
return 0;
}
//
// This is the main HTTP client connect work. Makes the connection
// and handles the protocol and reads the return headers. Needs
// to leave the stream at the start of the real data.
//
static int shoutcast_connect(struct shoutcast_session* session,
struct sockaddr_in* server, char* host_addr, const char* url)
{
int socket_handle;
int peer_handle;
int rc;
char mimeBuffer[256];
int timeout = HTTP_RCV_TIMEO;
if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
{
rt_kprintf( "ICY: SOCKET FAILED\n" );
return -1;
}
/* set recv timeout option */
setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void*)&timeout, sizeof(timeout));
peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
if ( peer_handle < 0 )
{
rt_kprintf( "ICY: CONNECT FAILED %i\n", peer_handle );
lwip_close(socket_handle);
return -1;
}
{
char *buf;
rt_uint32_t length;
buf = rt_malloc (512);
if (*url)
length = rt_snprintf(buf, 512, _shoutcast_get, url, host_addr);
else
length = rt_snprintf(buf, 512, _shoutcast_get, "/", host_addr);
rc = send(peer_handle, buf, length, 0);
rt_kprintf("SHOUTCAST request:\n%s", buf);
/* release buffer */
rt_free(buf);
}
/* read the header information */
while ( 1 )
{
// read a line from the header information.
rc = http_read_line(peer_handle, mimeBuffer, 100);
rt_kprintf(">>%s", mimeBuffer);
if ( rc < 0 )
{
lwip_close(socket_handle);
return rc;
}
// End of headers is a blank line. exit.
if (rc == 0) break;
if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
if(strstr(mimeBuffer, "ICY")) // First line of header, contains status code. Check for an error code
{
rc = shoutcast_is_error_header(mimeBuffer);
if(rc)
{
rt_kprintf("ICY: status code = %d!\n", rc);
lwip_close(socket_handle);
return -rc;
}
}
if (strstr(mimeBuffer, "HTTP/1."))
{
rc = http_is_error_header(mimeBuffer);
if(rc)
{
rt_kprintf("HTTP: status code = %d!\n", rc);
lwip_close(socket_handle);
return -rc;
}
}
if (strstr(mimeBuffer, "icy-name:"))
{
/* get name */
char* name;
name = mimeBuffer + strlen("icy-name:");
session->station_name = rt_strdup(name);
rt_kprintf("station name: %s\n", session->station_name);
}
if (strstr(mimeBuffer, "icy-br:"))
{
/* get bitrate */
session->bitrate = strtol(mimeBuffer + strlen("icy-br:"), RT_NULL, 10);
rt_kprintf("bitrate: %d\n", session->bitrate);
}
if (strstr(mimeBuffer, "icy-metaint:"))
{
/* get metaint */
session->metaint = strtol(mimeBuffer + strlen("icy-metaint:"), RT_NULL, 10);
rt_kprintf("metaint: %d\n", session->metaint);
}
if (strstr(mimeBuffer, "content-type:"))
{
/* check content-type */
if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
{
rt_kprintf("ICY content is not audio/mpeg.\n");
lwip_close(socket_handle);
return -1;
}
}
if (strstr(mimeBuffer, "Content-Type:"))
{
/* check content-type */
if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
{
rt_kprintf("ICY content is not audio/mpeg.\n");
lwip_close(socket_handle);
return -1;
}
}
}
// We've sent the request, and read the headers. SockHandle is
// now at the start of the main data read for a file io read.
return peer_handle;
}
#include <finsh.h>
rt_uint32_t total = 0;
FINSH_VAR_EXPORT(total, finsh_type_uint, total read in net);
struct shoutcast_session* shoutcast_session_open(const char* url)
{
int peer_handle = 0;
struct sockaddr_in server;
char *request, host_addr[32];
struct shoutcast_session* session;
total = 0;
session = (struct shoutcast_session*) rt_malloc(sizeof(struct shoutcast_session));
if(session == RT_NULL) return RT_NULL;
session->metaint = 0;
session->current_meta_chunk = 0;
session->bitrate = 0;
session->station_name = RT_NULL;
/* Check valid IP address and URL */
if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
{
rt_free(session);
return RT_NULL;
}
rt_kprintf("connect to: %s...\n", host_addr);
// Now we connect and initiate the transfer by sending a
// request header to the server, and receiving the response header
if((peer_handle = shoutcast_connect(session, &server, host_addr, request)) < 0)
{
rt_kprintf("SHOUTCAST: failed to connect to '%s'!\n", host_addr);
if (session->station_name != RT_NULL)
rt_free(session->station_name);
rt_free(session);
return RT_NULL;
}
// http connect returns valid socket. Save in handle list.
session->socket = peer_handle;
/* open successfully */
return session;
}
rt_size_t shoutcast_session_read(struct shoutcast_session* session, rt_uint8_t *buffer, rt_size_t length)
{
int bytesRead = 0;
int totalRead = 0;
int left = length;
static rt_uint32_t first_meta_size = 0;
// Read until: there is an error, we've read "size" bytes or the remote
// side has closed the connection.
do
{
bytesRead = recv(session->socket, buffer + totalRead, left, 0);
if(bytesRead <= 0)
{
rt_kprintf("no data on recv, len %d,err %d\n", bytesRead,
lwip_get_error(session->socket));
break;
}
total += bytesRead;
left -= bytesRead;
totalRead += bytesRead;
} while(left);
/* handle meta */
if (first_meta_size > 0)
{
/* skip meta data */
memmove(&buffer[0], &buffer[first_meta_size], totalRead - first_meta_size);
// rt_kprintf("remove meta: len %d\n", first_meta_size);
totalRead = totalRead - first_meta_size;
first_meta_size = 0;
session->current_meta_chunk = totalRead;
}
else
{
if (session->current_meta_chunk + totalRead == session->metaint)
{
rt_uint8_t meta_data;
recv(session->socket, &meta_data, 1, 0);
/* remove meta data in next packet */
first_meta_size = meta_data * 16;
session->current_meta_chunk = 0;
// rt_kprintf("get meta: len %d\n", first_meta_size);
}
else if (session->current_meta_chunk + totalRead > session->metaint)
{
int meta_length, next_chunk_length;
// rt_kprintf("c: %d, total: %d, m: %d\n", session->current_meta_chunk, totalRead, session->metaint);
/* get the length of meta data */
meta_length = buffer[session->metaint - session->current_meta_chunk] * 16;
next_chunk_length = totalRead - (session->metaint - session->current_meta_chunk) -
(meta_length + 1);
// rt_kprintf("l: %d, n: %d\n", meta_length, next_chunk_length);
/* skip meta data */
memmove(&buffer[session->metaint - session->current_meta_chunk],
&buffer[session->metaint - session->current_meta_chunk + meta_length + 1],
next_chunk_length);
/* set new current meta chunk */
session->current_meta_chunk = next_chunk_length;
totalRead = totalRead - (meta_length + 1);
}
else
{
session->current_meta_chunk += totalRead;
}
}
return totalRead;
}
rt_off_t shoutcast_session_seek(struct shoutcast_session* session, rt_off_t offset, int mode)
{
/* not support seek yet */
return 0;
}
int shoutcast_session_close(struct shoutcast_session* session)
{
lwip_close(session->socket);
if (session->station_name != RT_NULL)
rt_free(session->station_name);
rt_free(session);
return 0;
}
#include <finsh.h>
void http_test(char* url)
{
struct http_session* session;
char buffer[80];
rt_size_t length;
session = http_session_open(url);
if (session == RT_NULL)
{
rt_kprintf("open http session failed\n");
return;
}
do
{
rt_memset(buffer, 0, sizeof(buffer));
length = http_session_read(session, (rt_uint8_t*)buffer, sizeof(buffer));
rt_kprintf(buffer);rt_kprintf("\n");
} while (length > 0);
http_session_close(session);
}
FINSH_FUNCTION_EXPORT(http_test, http client test);
void shoutcast_test(char* url)
{
struct shoutcast_session* session;
session = shoutcast_session_open(url);
if (session == RT_NULL)
{
rt_kprintf("open shoutcast session failed\n");
return;
}
shoutcast_session_close(session);
}
FINSH_FUNCTION_EXPORT(shoutcast_test, shoutcast client test);