make fetch less dumb

This commit is contained in:
Kevin Lange 2015-08-03 22:24:16 -07:00
parent 5df1b59dc2
commit 5f0fd70fd3

View File

@ -1,11 +1,45 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 512
struct http_req {
char domain[SIZE];
char path[SIZE];
};
void parse_url(char * d, struct http_req * r) {
if (strstr(d, "http://") == d) {
d += strlen("http://");
char * s = strstr(d, "/");
if (!s) {
strcpy(r->domain, d);
strcpy(r->path, "");
} else {
*s = 0;
s++;
strcpy(r->domain, d);
strcpy(r->path, s);
}
} else {
fprintf(stderr, "sorry, can't parse %s\n", d);
exit(1);
}
}
int main(int argc, char * argv[]) {
if (argc < 2) return 1;
struct http_req my_req;
parse_url(argv[1], &my_req);
char file[100];
sprintf(file, "/dev/net/%s", argv[1]);
sprintf(file, "/dev/net/%s", my_req.domain);
FILE * f = fopen(file,"r+");
@ -15,14 +49,15 @@ int main(int argc, char * argv[]) {
}
fprintf(f,
"GET / HTTP/1.0\r\n"
"GET /%s HTTP/1.0\r\n"
"User-Agent: curl/7.35.0\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"
"\r\n", argv[1]);
"\r\n", my_req.path, my_req.domain);
while (!feof(f)) {
char buf[4096];
memset(buf, 0, sizeof(buf));
fgets(buf, 4096, f);
fprintf(stdout, "%s", buf);
}