2 * Acess2 Command-line HTTP Client (wget)
14 #include <acess/sys.h> // _SysDebug
26 int main(int argc, char *argv[]);
27 int _ParseHeaderLine(char *Line, int State, size_t *Size);
28 void writef(int fd, const char *format, ...);
30 int main(int argc, char *argv[])
33 gasURLs = malloc( (argc - 1) * sizeof(*gasURLs) );
36 for(int i = 1; i < argc; i ++ )
41 gasURLs[giNumURLs++] = arg;
43 else if( arg[1] != '-') {
52 for( int i = 0; i < giNumURLs; i ++ )
55 tURI *uri = URI_Parse(gasURLs[i]);
56 struct addrinfo *addrinfo;
59 fprintf(stderr, "'%s' is not a valid URL", gasURLs[i]);
63 printf("Proto: %s, Host: %s, Path: %s\n", uri->Proto, uri->Host, uri->Path);
65 if( uri->Path[0] == '\0' || uri->Path[strlen(uri->Path)-1] == '/' )
66 outfile = "index.html";
68 outfile = strrchr(uri->Path, '/');
75 if( strcmp(uri->Proto, "http") == 0 ) {
78 else if( strcmp(uri->Proto, "https") == 0 ) {
83 fprintf(stderr, "Unknown protocol '%s'\n", uri->Proto);
88 if( proto != PROTO_HTTP ) {
89 fprintf(stderr, "TODO: Support protocols other than HTTP\n");
94 rv = getaddrinfo(uri->Host, "http", NULL, &addrinfo);
96 fprintf(stderr, "Unable to resolve %s: %s\n", uri->Host, gai_strerror(rv));
100 for( struct addrinfo *addr = addrinfo; addr != NULL; addr = addr->ai_next )
103 // TODO: Convert to POSIX/BSD
104 // NOTE: using addr->ai_addr will break for IPv6, as there is more info before the address
107 printf("Attempting [%s]:80\n", Net_PrintAddress(addr->ai_family, addr->ai_addr->sa_data));
109 sock = Net_OpenSocket_TCPC(addr->ai_family, addr->ai_addr->sa_data, 80);
114 _SysDebug("Connected as %i", sock);
116 writef(sock, "GET /%s HTTP/1.1\r\n", uri->Path);
117 writef(sock, "Host: %s\r\n", uri->Host);
118 // writef(sock, "Accept-Encodings: */*\r\n");
119 writef(sock, "User-Agent: awget/0.1 (Acess2)\r\n");
120 writef(sock, "\r\n");
123 char inbuf[BUFSIZ+1];
124 size_t offset = 0, len = 0;
126 size_t bytes_seen = 0;
127 size_t bytes_wanted = -1; // invalid
131 while( state == 0 || state == 1 )
133 if( offset == BUFSIZ ) {
139 char *eol = strchr(inbuf, '\n');
140 // No end of line char? read some more
142 // TODO: Handle -1 return
143 len += read(sock, inbuf + offset, BUFSIZ - 1 - offset);
147 // abuse offset as the end of the string
148 offset = (eol - inbuf) + 1;
153 if( eol - 1 >= inbuf )
157 state = _ParseHeaderLine(inbuf, state, &bytes_wanted);
159 // Move unused data down in memory
161 memmove( inbuf, inbuf + offset, BUFSIZ - offset );
167 _SysDebug("RXing %i bytes to '%s'", bytes_wanted, outfile);
168 int outfd = open(outfile, O_WRONLY|O_CREAT, 0666);
170 fprintf(stderr, "Unable to open '%s' for writing\n", outfile);
174 // Write the remainder of the buffer
177 write(outfd, inbuf, len);
179 _SysDebug("%i/%i bytes done", bytes_seen, bytes_wanted);
180 } while( bytes_seen < bytes_wanted && (len = read(sock, inbuf, sizeof(inbuf))) > 0 );
185 _SysDebug("Closing socket");
196 int _ParseHeaderLine(char *Line, int State, size_t *Size)
198 _SysDebug("Header - %s", Line);
199 // First line (Status and version)
202 // HACK - assumes HTTP/1.1 (well, 9 chars before status)
203 switch( atoi(Line + 9) )
209 fprintf(stderr, "Unknown HTTP status - %s\n", Line + 9);
214 else if( Line[0] == '\0' )
222 char *colon = strchr(Line, ':');
223 if(colon == NULL) return 1;
227 if( strcmp(Line, "Content-Length") == 0 ) {
231 printf("Ignorning header '%s' = '%s'\n", Line, value);
238 void writef(int fd, const char *format, ...)
243 va_start(args, format);
244 len = vsnprintf(NULL, 0, format, args);
248 va_start(args, format);
249 vsnprintf(data, len+1, format, args);
252 write(fd, data, len);