summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c58
1 files changed, 39 insertions, 19 deletions
diff --git a/util.c b/util.c
index 4d2b04c..04e80b4 100644
--- a/util.c
+++ b/util.c
@@ -3,10 +3,15 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include "util.h"
+#ifndef WINVER
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
-#include "util.h"
+#else
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
/* plundered from suckless' sic */
int dial(char *host, char *port) {
@@ -14,6 +19,14 @@ int dial(char *host, char *port) {
int srv;
struct addrinfo *res, *r;
+ #ifdef WINVER
+ WSADATA w;
+ if(WSAStartup(MAKEWORD(2,2), &w)!= 0) {
+ fprintf(stderr, "error: failed to start winsock\n");
+ return -1;
+ }
+ #endif
+
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
@@ -39,35 +52,42 @@ int dial(char *host, char *port) {
int get(char *host, char *path, char *sendcookie, char *savecookie, char **buf) {
size_t l, res;
int fd, i, p;
- char h[HEADERMAX] = "\0";
+ char h[HDRMAX] = "";
char c[COOKIEMAX] = "";
+ char t[BUFSIZ];
+ char *t2;
char m[256];
- FILE *srv;
if((fd = dial(host, "80")) == -1) return 0;
- srv = fdopen(fd, "r+");
- if(sendcookie)
+ if(sendcookie && sendcookie[0])
snprintf(c, COOKIEMAX, "\r\nCookie: %s", sendcookie);
- fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getxbook-"VERSION \
- " (not mozilla)\r\nHost: %s%s\r\n\r\n", path, host, c);
- fflush(srv);
+ snprintf(h, HDRMAX, "GET %s HTTP/1.0\r\nUser-Agent: getxbook-"VERSION \
+ " (not mozilla)\r\nHost: %s%s\r\n\r\n", path, host, c);
+ if(!send(fd, h, HDRMAX, 0)) return 0;
+ *buf = NULL;
+ l = 0;
snprintf(m, 256, "Set-Cookie: %%%ds;", COOKIEMAX-1);
-
- while(h[0] != '\r') {
- if(!fgets(h, HEADERMAX, srv)) return 0;
- if(sscanf(h, "HTTP/%d.%d %d", &i, &i, &p) == 3 && p != 200)
+ while((res = recv(fd, t, BUFSIZ, 0)) > 0) {
+ if(sscanf(t, "HTTP/%d.%d %d", &i, &i, &p) == 3 && p != 200)
return 0;
- if(savecookie != NULL && sscanf(h, m, c))
+ if(savecookie != NULL &&
+ (t2 = strstr(t, "Set-Cookie: ")) != NULL && sscanf(t2, m, c))
strncat(savecookie, c, COOKIEMAX);
+ if((t2 = strstr(t, "\r\n\r\n")) != NULL) {
+ t2+=4;
+ l = res - (t2 - t);
+ *buf = malloc(sizeof(char *) * l);
+ memcpy(*buf, t2, l);
+ break;
+ }
}
- *buf = malloc(sizeof(char *) * BUFSIZ);
- for(l=0; (res = fread(*buf+l, 1, BUFSIZ, srv)) > 0; l+=res)
+ *buf = realloc(*buf, sizeof(char *) * (l+BUFSIZ));
+ for(; (res = recv(fd, *buf+l, BUFSIZ, 0)) > 0; l+=res)
*buf = realloc(*buf, sizeof(char *) * (l+BUFSIZ));
- fclose(srv);
return l;
}
@@ -80,13 +100,13 @@ int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *s
fprintf(stderr, "Could not download %s\n", url);
return 1;
}
- if((f = fopen(savepath, "w")) == NULL) {
+ if((f = fopen(savepath, "wb")) == NULL) {
fprintf(stderr, "Could not create file %s\n", savepath);
free(buf); return 1;
}
- for(i=0; i < l; i+=512)
- if(!fwrite(buf+i, l-i > 512 ? 512 : l-i, 1, f)) {
+ for(i=0; i < l; i+=BUFSIZ)
+ if(!fwrite(buf+i, l-i > BUFSIZ ? BUFSIZ : l-i, 1, f)) {
fprintf(stderr, "Error writing file %s\n", savepath);
free(buf); fclose(f); return 1;
}