]> git.zerfleddert.de Git - rigol/commitdiff
be a bit more HTTP compliant...
authorMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 19:38:16 +0000 (21:38 +0200)
committerMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 19:38:16 +0000 (21:38 +0200)
rigold.c

index 8f73431b8967a628c9bdf388a0e730dc462c9f03..6e383b71fb2322555fcf0ff384c95909cdd53cb5 100644 (file)
--- a/rigold.c
+++ b/rigold.c
@@ -11,6 +11,8 @@
 #include "scope.h"
 #include "commands.h"
 
 #include "scope.h"
 #include "commands.h"
 
+#define TIMEOUT 4
+
 static int send_binary(int s, char *buf, int len)
 {
        int ret;
 static int send_binary(int s, char *buf, int len)
 {
        int ret;
@@ -35,7 +37,7 @@ static int send_text(int s, char *buf)
 
 static void serve_index(int s, struct scope *sc)
 {
 
 static void serve_index(int s, struct scope *sc)
 {
-       send_text(s, "HTTP/1.0 200 OK");
+       send_text(s, "HTTP/1.0 200 OK\n");
        send_text(s, "Content-type: text/html\n\n");
        send_text(s, "<html><head><title>");
        send_text(s, scope_idn(sc));
        send_text(s, "Content-type: text/html\n\n");
        send_text(s, "<html><head><title>");
        send_text(s, scope_idn(sc));
@@ -58,7 +60,7 @@ static void serve_lcd(int s, struct scope *sc)
                return;
 
 
                return;
 
 
-       send_text(s, "HTTP/1.0 200 OK");
+       send_text(s, "HTTP/1.0 200 OK\n");
        send_text(s, "Content-type: image/png\n");
        snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
        send_text(s, buf);
        send_text(s, "Content-type: image/png\n");
        snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
        send_text(s, buf);
@@ -66,16 +68,37 @@ static void serve_lcd(int s, struct scope *sc)
        free(png);
 }
 
        free(png);
 }
 
+int is_eor(char *buf)
+{
+       int eor = 0;
+
+       /* This does not completely work, when the request is split
+          in multiple packets... */
+       if (strstr(buf, "\x0d\x0a\x0d\x0a")) {
+               eor = 1;
+       } else if (strstr(buf, "\x0a\x0a")) {
+               eor = 1;
+       } else if (strcmp(buf, "\x0d\x0a") == 0) {
+               eor = 1;
+       } else if (strcmp(buf, "\x0a") == 0) {
+               eor = 1;
+       }
+
+       return eor;
+}
+
+
 static void parse_request(int s, struct scope *sc)
 {
        int ret;
 static void parse_request(int s, struct scope *sc)
 {
        int ret;
-       char buf[1024];
-       char file[1024];
+       int eor;
+       char buf[4096];
+       char file[4096];
        const char delim[] = " \t\x0d\x0a";
        const char delim[] = " \t\x0d\x0a";
-       const char crlf[] = "\x0d\x0a";
        char *saveptr;
        char *token;
 
        char *saveptr;
        char *token;
 
+       alarm(TIMEOUT);
        ret=read(s, buf, sizeof(buf)-1);
        if (ret == -1) {
                perror("read");
        ret=read(s, buf, sizeof(buf)-1);
        if (ret == -1) {
                perror("read");
@@ -83,6 +106,8 @@ static void parse_request(int s, struct scope *sc)
        }
        buf[ret] = 0;
 
        }
        buf[ret] = 0;
 
+       eor = is_eor(buf);
+
        token = strtok_r(buf, delim, &saveptr);
        if (token == NULL)
                return;
        token = strtok_r(buf, delim, &saveptr);
        if (token == NULL)
                return;
@@ -93,21 +118,17 @@ static void parse_request(int s, struct scope *sc)
        bzero(&file, sizeof(file));
        strncpy(file, token, sizeof(file)-1);
 
        bzero(&file, sizeof(file));
        strncpy(file, token, sizeof(file)-1);
 
-       do {
-               token = strtok_r(NULL, crlf, &saveptr);
-               /* TODO: FIXME */
-               #if 0
-               if (token == NULL) {
-                       ret=read(s, buf, sizeof(buf)-1);
-                       if (ret == -1) {
-                               perror("read");
-                               return;
-                       }
-                       buf[ret] = 0;
-                       token = strtok_r(buf, crlf, &saveptr);
+       while (!eor) {
+               alarm(TIMEOUT);
+               ret=read(s, buf, sizeof(buf)-1);
+               if (ret == -1) {
+                       perror("read");
+                       return;
                }
                }
-               #endif
-       } while(token != NULL);
+               buf[ret] = 0;
+               eor = is_eor(buf);
+       }
+       alarm(0);
 
        if (strcmp("/", file) == 0) {
                serve_index(s, sc);
 
        if (strcmp("/", file) == 0) {
                serve_index(s, sc);
@@ -118,7 +139,14 @@ static void parse_request(int s, struct scope *sc)
 
 void sighandler(int sig)
 {
 
 void sighandler(int sig)
 {
-       printf("Signal %d received\n", sig);
+       switch(sig) {
+               case SIGPIPE:
+                       break;
+               case SIGALRM:
+               default:
+                       printf("Signal %d received\n", sig);
+                       break;
+       }
 }
 
 int main(int argc, char **argv) 
 }
 
 int main(int argc, char **argv) 
@@ -145,6 +173,13 @@ int main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
                exit(EXIT_FAILURE);
        }
 
+       bzero(&act, sizeof(act));
+       act.sa_handler = sighandler;
+       if (sigaction(SIGALRM, &act, NULL) == -1) {
+               perror("sigaction");
+               exit(EXIT_FAILURE);
+       }
+
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(EXIT_FAILURE);
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(EXIT_FAILURE);
Impressum, Datenschutz