00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <conio.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include <assert.h>
00023 #include "inet/httpget.h"
00024 #include "inet/base64.h"
00025 #include "inet/dns.h"
00026 #include "inet/url.h"
00027 #include "sc12/tcpip.h"
00028 #include "vs1001.cpp"
00029 #include "misc.h"
00030
00031 int http_get(const char *urlString, char *result, unsigned maxlen)
00032 {
00033 URL *url = parse_url(urlString);
00034
00035 unsigned long ipnumber, ttl;
00036 char ipaddr[16];
00037
00038 if (inet_addr(url->host, &ipnumber)) {
00039 int ret = gethostbyname(NULL, url->host, &ttl, &ipnumber, ipaddr);
00040 if (!ret)
00041 printf("can't resolve ip address!\n");
00042 } else {
00043 strcpy( ipaddr, url->host );
00044 }
00045
00046 printf("resolved to %s.\n", ipaddr);
00047
00048 char *authcode = NULL;
00049 if (url->user) {
00050 char str[64];
00051 char *s = str;
00052
00053 s += sprintf(s, "%s", url->user);
00054 if (url->pass)
00055 s += sprintf(s, ":%s", url->pass);
00056
00057 assert((s-str)<sizeof(str));
00058
00059 printf("str=\"%s\"", str);
00060
00061 authcode = base64encode(str, strlen(str), NULL);
00062 }
00063
00064 if (!url->port)
00065 url->port = 80;
00066
00067 char request[512];
00068 char *r = request;
00069
00070 r += sprintf(r,
00071 "GET /%s HTTP/1.1\r\n"
00072 "Host: %s:%u\r\n"
00073 "User-Agent: mp3v2 client 1.1, visit www.kreapc.de\r\n"
00074 "Connection: Keep-Alive\r\n",
00075 url->path ? url->path : "",
00076 url->host, url->port
00077 );
00078 if (authcode)
00079 r += sprintf(r, "Authorization: Basic %s\r\n", authcode);
00080 r += sprintf(r, "\r\n");
00081
00082 assert((r-request) < sizeof(request));
00083
00084 printf("request --------\n%s", request);
00085
00086 int error;
00087 int sd;
00088 char *buf1, *buf2;
00089 unsigned rcvd;
00090 char *endheader;
00091 unsigned long t1, t2, t, total=0;
00092
00093 sd = tcp_connect(ipaddr, 0, url->port, &error);
00094 if (error) {
00095 printf("error %d while connecting to \"%s\", port %u.\n", error, ipaddr, url->port);
00096 return 0;
00097 }
00098
00099 send(sd, request, strlen(request), 0, &error);
00100 if (error) {
00101 printf( "error %d while sending request.\n", error );
00102 return 0;
00103
00104 }
00105
00106 buf1 = &result[ 0];
00107 buf2 = &result[32768];
00108
00109 VS_Reset();
00110
00111 rcvd = recv(sd, result, 1024, MSG_TIMEOUT, 10000L, &error);
00112 endheader = strstr(result, "\r\n\r\n");
00113
00114 if (endheader) {
00115 *endheader = '\0';
00116 printf("Header:\n%s",result);
00117 endheader += 4;
00118 rcvd -= (int)(endheader-result);
00119 VS_PlayMpegAsync(endheader, rcvd, NULL, 0 );
00120 }
00121
00122 while (!kbhit() && !error) {
00123 rcvd = 0;
00124 while (rcvd < 11616*2 && !error)
00125 rcvd += recv(sd, &buf1[rcvd], 4096, MSG_TIMEOUT, 10000L, &error);
00126 total += rcvd;
00127 printf("%d bytes.\n", rcvd);
00128
00129 while (VS_IsPlaying());
00130 VS_PlayMpegAsync(buf1, rcvd, NULL, 0);
00131 rcvd = 0;
00132 while (rcvd < 11616*2 && !error)
00133 rcvd += recv(sd, &buf2[rcvd], 4096, MSG_TIMEOUT, 10000L, &error);
00134 printf("%d bytes.\n", rcvd);
00135 while (VS_IsPlaying());
00136 VS_PlayMpegAsync(buf2, rcvd, NULL, 0);
00137 }
00138
00139 EndTransfer:
00140 closesocket(sd, &error);
00141 if (error)
00142 printf( "error %d while closing connection to \"%s\", port %u.\n", error, ipaddr, url->port );
00143
00144
00145 return 0;
00146 }