X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/7a226bb8f5a070842eed562c04df6b79970a4e4b..04d383a4a09bfaa3e54b39d36dd8ce6f80afe725:/rigold.c diff --git a/rigold.c b/rigold.c index 8f73431..111c5d6 100644 --- a/rigold.c +++ b/rigold.c @@ -11,6 +11,8 @@ #include "scope.h" #include "commands.h" +#define TIMEOUT 4 + static int send_binary(int s, char *buf, int len) { int ret; @@ -33,14 +35,43 @@ static int send_text(int s, char *buf) return send_binary(s, buf, strlen(buf)); } -static void serve_index(int s, struct scope *sc) +static void serve_index(int s, struct scope *sc, char *param) { - 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, ""); send_text(s, scope_idn(sc)); send_text(s, "\n"); - send_text(s, "\n"); + send_text(s, "\n"); + send_text(s, "
\n"); + send_text(s, "
\n"); + send_text(s, "\n"); + send_text(s, "\n"); + send_text(s, "
\n"); + if (param) { + unsigned char buf[1024*1024]; + int res; + + claimscope(sc); + if (strchr (param, '?')) { + res = sendscpi(sc, param, buf, sizeof(buf)); + send_text(s, "
< ");
+			send_text(s, param);
+			send_text(s, "\n> ");
+			send_binary(s, (char*)buf, res);
+			send_text(s, "
\n"); + } else { + sendscpi(sc, param, NULL, 0); + } + releasescope(sc); + } send_text(s, "\n"); } @@ -58,7 +89,7 @@ static void serve_lcd(int s, struct scope *sc) 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); @@ -66,16 +97,48 @@ static void serve_lcd(int s, struct scope *sc) free(png); } +static void serve_404(int s) +{ + send_text(s, "HTTP/1.0 404 Not found\n"); + send_text(s, "Content-type: text/html\n\n"); + send_text(s, "404"); + send_text(s, "\n"); + send_text(s, "

404

"); + send_text(s, "\n"); +} + +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; - char buf[1024]; - char file[1024]; + int eor; + char buf[4096]; + char file[4096]; const char delim[] = " \t\x0d\x0a"; - const char crlf[] = "\x0d\x0a"; char *saveptr; char *token; + char *param = NULL; + alarm(TIMEOUT); ret=read(s, buf, sizeof(buf)-1); if (ret == -1) { perror("read"); @@ -83,6 +146,8 @@ static void parse_request(int s, struct scope *sc) } buf[ret] = 0; + eor = is_eor(buf); + token = strtok_r(buf, delim, &saveptr); if (token == NULL) return; @@ -93,32 +158,57 @@ static void parse_request(int s, struct scope *sc) 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; + } + buf[ret] = 0; + eor = is_eor(buf); + } + alarm(0); + + param = strchr(file, '?'); + if (param) { + *param = 0; + param++; + + while ((token = strchr(param, '%')) != NULL) { + char buf[3]; + + if (strlen(token+1) < 2) + break; + + strncpy(buf, token+1, 3); + buf[2] = 0; + + *token = (char)strtoul(buf, NULL, 16); + memmove(token+1, token+3, strlen(token)-2); + } - #endif - } while(token != NULL); + } if (strcmp("/", file) == 0) { - serve_index(s, sc); - } else if (strcmp("/lcd.png", file) == 0) { + serve_index(s, sc, param); + } else if (strcmp("/cgi-bin/lcd", file) == 0) { serve_lcd(s, sc); + } else { + serve_404(s); } } 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) @@ -145,6 +235,13 @@ int main(int argc, char **argv) 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);