static int http_parse_request(HTTPContext *c)

{

    char *p;

    int post;

    char cmd[32];

    char info[1024], *filename;

    char url[1024], *q;

    char protocol[32];

    char msg[1024];

    const char *mime_type;

    FFStream *stream;



    p = c->buffer;

    q = cmd;

    while (!isspace(*p) && *p != '\0') {

        if ((q - cmd) < sizeof(cmd) - 1)

            *q++ = *p;

        p++;

    }

    *q = '\0';

    if (!strcmp(cmd, "GET"))

        post = 0;

    else if (!strcmp(cmd, "POST"))

        post = 1;

    else

        return -1;



    while (isspace(*p)) p++;

    q = url;

    while (!isspace(*p) && *p != '\0') {

        if ((q - url) < sizeof(url) - 1)

            *q++ = *p;

        p++;

    }

    *q = '\0';



    while (isspace(*p)) p++;

    q = protocol;

    while (!isspace(*p) && *p != '\0') {

        if ((q - protocol) < sizeof(protocol) - 1)

            *q++ = *p;

        p++;

    }

    *q = '\0';

    if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))

        return -1;

    

    /* find the filename and the optional info string in the request */

    p = url;

    if (*p == '/')

        p++;

    filename = p;

    p = strchr(p, '?');

    if (p) {

        strcpy(info, p);

        *p = '\0';

    } else {

        info[0] = '\0';

    }



    stream = first_stream;

    while (stream != NULL) {

        if (!strcmp(stream->filename, filename))

            break;

        stream = stream->next;

    }

    if (stream == NULL) {

        sprintf(msg, "File '%s' not found", url);

        goto send_error;

    }

    c->stream = stream;

    

    /* should do it after so that the size can be computed */

    {

        char buf1[32], buf2[32], *p;

        time_t ti;

        /* XXX: reentrant function ? */

        p = inet_ntoa(c->from_addr.sin_addr);

        strcpy(buf1, p);

        ti = time(NULL);

        p = ctime(&ti);

        strcpy(buf2, p);

        p = buf2 + strlen(p) - 1;

        if (*p == '\n')

            *p = '\0';

        http_log("%s - - [%s] \"%s %s %s\" %d %d\n", 

                 buf1, buf2, cmd, url, protocol, 200, 1024);

    }



    /* XXX: add there authenticate and IP match */



    if (post) {

        /* if post, it means a feed is being sent */

        if (!stream->is_feed) {

            sprintf(msg, "POST command not handled");

            goto send_error;

        }

        if (http_start_receive_data(c) < 0) {

            sprintf(msg, "could not open feed");

            goto send_error;

        }

        c->http_error = 0;

        c->state = HTTPSTATE_RECEIVE_DATA;

        return 0;

    }



    if (c->stream->stream_type == STREAM_TYPE_STATUS)

        goto send_stats;



    /* open input stream */

    if (open_input_stream(c, info) < 0) {

        sprintf(msg, "Input stream corresponding to '%s' not found", url);

        goto send_error;

    }



    /* prepare http header */

    q = c->buffer;

    q += sprintf(q, "HTTP/1.0 200 OK\r\n");

    mime_type = c->stream->fmt->mime_type;

    if (!mime_type)

        mime_type = "application/x-octet_stream";

    q += sprintf(q, "Pragma: no-cache\r\n");



    /* for asf, we need extra headers */

    if (!strcmp(c->stream->fmt->name,"asf")) {

        q += sprintf(q, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=1234\r\nPragma: features=\"broadcast\"\r\n");

        mime_type = "application/octet-stream";

    }

    q += sprintf(q, "Content-Type: %s\r\n", mime_type);

    q += sprintf(q, "\r\n");

    

    /* prepare output buffer */

    c->http_error = 0;

    c->buffer_ptr = c->buffer;

    c->buffer_end = q;

    c->state = HTTPSTATE_SEND_HEADER;

    return 0;

 send_error:

    c->http_error = 404;

    q = c->buffer;

    q += sprintf(q, "HTTP/1.0 404 Not Found\r\n");

    q += sprintf(q, "Content-type: %s\r\n", "text/html");

    q += sprintf(q, "\r\n");

    q += sprintf(q, "<HTML>\n");

    q += sprintf(q, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");

    q += sprintf(q, "<BODY>%s</BODY>\n", msg);

    q += sprintf(q, "</HTML>\n");



    /* prepare output buffer */

    c->buffer_ptr = c->buffer;

    c->buffer_end = q;

    c->state = HTTPSTATE_SEND_HEADER;

    return 0;

 send_stats:

    compute_stats(c);

    c->http_error = 200; /* horrible : we use this value to avoid

                            going to the send data state */

    c->state = HTTPSTATE_SEND_HEADER;

    return 0;

}

static int http_parse_request(HTTPContext *c)

{

    char *p;

    int post;

    char cmd[32];

    char info[1024], *filename;

    char url[1024], *q;

    char protocol[32];

    char msg[1024];

    const char *mime_type;

    FFStream *stream;



    p = c->buffer;

    q = cmd;

    while (!isspace(*p) && *p != '\0') {

        if ((q - cmd) < sizeof(cmd) - 1)

            *q++ = *p;

        p++;

    }

    *q = '\0';

    if (!strcmp(cmd, "GET"))

        post = 0;

    else if (!strcmp(cmd, "POST"))

        post = 1;

    else

        return -1;



    while (isspace(*p)) p++;

    q = url;

    while (!isspace(*p) && *p != '\0') {

        if ((q - url) < sizeof(url) - 1)

            *q++ = *p;

        p++;

    }

    *q = '\0';



    while (isspace(*p)) p++;

    q = protocol;

    while (!isspace(*p) && *p != '\0') {

        if ((q - protocol) < sizeof(protocol) - 1)

            *q++ = *p;

        p++;

    }

    *q = '\0';

    if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))

        return -1;

    

    

    p = url;

    if (*p == '/')

        p++;

    filename = p;

    p = strchr(p, '?');

    if (p) {

        strcpy(info, p);

        *p = '\0';

    } else {

        info[0] = '\0';

    }



    stream = first_stream;

    while (stream != NULL) {

        if (!strcmp(stream->filename, filename))

            break;

        stream = stream->next;

    }

    if (stream == NULL) {

        sprintf(msg, "File '%s' not found", url);

        goto send_error;

    }

    c->stream = stream;

    

    

    {

        char buf1[32], buf2[32], *p;

        time_t ti;

        

        p = inet_ntoa(c->from_addr.sin_addr);

        strcpy(buf1, p);

        ti = time(NULL);

        p = ctime(&ti);

        strcpy(buf2, p);

        p = buf2 + strlen(p) - 1;

        if (*p == '\n')

            *p = '\0';

        http_log("%s - - [%s] \"%s %s %s\" %d %d\n", 

                 buf1, buf2, cmd, url, protocol, 200, 1024);

    }



    



    if (post) {

        

        if (!stream->is_feed) {

            sprintf(msg, "POST command not handled");

            goto send_error;

        }

        if (http_start_receive_data(c) < 0) {

            sprintf(msg, "could not open feed");

            goto send_error;

        }

        c->http_error = 0;

        c->state = HTTPSTATE_RECEIVE_DATA;

        return 0;

    }



    if (c->stream->stream_type == STREAM_TYPE_STATUS)

        goto send_stats;



    

    if (open_input_stream(c, info) < 0) {

        sprintf(msg, "Input stream corresponding to '%s' not found", url);

        goto send_error;

    }



    

    q = c->buffer;

    q += sprintf(q, "HTTP/1.0 200 OK\r\n");

    mime_type = c->stream->fmt->mime_type;

    if (!mime_type)

        mime_type = "application/x-octet_stream";

    q += sprintf(q, "Pragma: no-cache\r\n");



    

    if (!strcmp(c->stream->fmt->name,"asf")) {

        q += sprintf(q, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=1234\r\nPragma: features=\"broadcast\"\r\n");

        mime_type = "application/octet-stream";

    }

    q += sprintf(q, "Content-Type: %s\r\n", mime_type);

    q += sprintf(q, "\r\n");

    

    

    c->http_error = 0;

    c->buffer_ptr = c->buffer;

    c->buffer_end = q;

    c->state = HTTPSTATE_SEND_HEADER;

    return 0;

 send_error:

    c->http_error = 404;

    q = c->buffer;

    q += sprintf(q, "HTTP/1.0 404 Not Found\r\n");

    q += sprintf(q, "Content-type: %s\r\n", "text/html");

    q += sprintf(q, "\r\n");

    q += sprintf(q, "<HTML>\n");

    q += sprintf(q, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");

    q += sprintf(q, "<BODY>%s</BODY>\n", msg);

    q += sprintf(q, "</HTML>\n");



    

    c->buffer_ptr = c->buffer;

    c->buffer_end = q;

    c->state = HTTPSTATE_SEND_HEADER;

    return 0;

 send_stats:

    compute_stats(c);

    c->http_error = 200; 



    c->state = HTTPSTATE_SEND_HEADER;

    return 0;

}

        strcpy(info, p);     c->stream = stream;         char buf1[32], buf2[32], *p;         time_t ti;         p = inet_ntoa(c->from_addr.sin_addr);         strcpy(buf1, p);         ti = time(NULL);         p = ctime(&ti);         strcpy(buf2, p);         p = buf2 + strlen(p) - 1;         if (*p == '\n')             *p = '\0';         http_log("%s - - [%s] \"%s %s %s\" %d %d\n",                   buf1, buf2, cmd, url, protocol, 200, 1024);         mime_type = "application/octet-stream";
static int FUNC_0(HTTPContext *VAR_0)

{

    char *VAR_13;

    int VAR_2;

    char VAR_3[32];

    char VAR_4[1024], *VAR_5;

    char VAR_6[1024], *VAR_7;

    char VAR_8[32];

    char VAR_9[1024];

    const char *VAR_10;

    FFStream *stream;



    VAR_13 = VAR_0->buffer;

    VAR_7 = VAR_3;

    while (!isspace(*VAR_13) && *VAR_13 != '\0') {

        if ((VAR_7 - VAR_3) < sizeof(VAR_3) - 1)

            *VAR_7++ = *VAR_13;

        VAR_13++;

    }

    *VAR_7 = '\0';

    if (!strcmp(VAR_3, "GET"))

        VAR_2 = 0;

    else if (!strcmp(VAR_3, "POST"))

        VAR_2 = 1;

    else

        return -1;



    while (isspace(*VAR_13)) VAR_13++;

    VAR_7 = VAR_6;

    while (!isspace(*VAR_13) && *VAR_13 != '\0') {

        if ((VAR_7 - VAR_6) < sizeof(VAR_6) - 1)

            *VAR_7++ = *VAR_13;

        VAR_13++;

    }

    *VAR_7 = '\0';



    while (isspace(*VAR_13)) VAR_13++;

    VAR_7 = VAR_8;

    while (!isspace(*VAR_13) && *VAR_13 != '\0') {

        if ((VAR_7 - VAR_8) < sizeof(VAR_8) - 1)

            *VAR_7++ = *VAR_13;

        VAR_13++;

    }

    *VAR_7 = '\0';

    if (strcmp(VAR_8, "HTTP/1.0") && strcmp(VAR_8, "HTTP/1.1"))

        return -1;

    

    

    VAR_13 = VAR_6;

    if (*VAR_13 == '/')

        VAR_13++;

    VAR_5 = VAR_13;

    VAR_13 = strchr(VAR_13, '?');

    if (VAR_13) {

        strcpy(VAR_4, VAR_13);

        *VAR_13 = '\0';

    } else {

        VAR_4[0] = '\0';

    }



    stream = first_stream;

    while (stream != NULL) {

        if (!strcmp(stream->VAR_5, VAR_5))

            break;

        stream = stream->next;

    }

    if (stream == NULL) {

        sprintf(VAR_9, "File '%s' not found", VAR_6);

        goto send_error;

    }

    VAR_0->stream = stream;

    

    

    {

        char VAR_11[32], VAR_12[32], *VAR_13;

        time_t ti;

        

        VAR_13 = inet_ntoa(VAR_0->from_addr.sin_addr);

        strcpy(VAR_11, VAR_13);

        ti = time(NULL);

        VAR_13 = ctime(&ti);

        strcpy(VAR_12, VAR_13);

        VAR_13 = VAR_12 + strlen(VAR_13) - 1;

        if (*VAR_13 == '\n')

            *VAR_13 = '\0';

        http_log("%s - - [%s] \"%s %s %s\" %d %d\n", 

                 VAR_11, VAR_12, VAR_3, VAR_6, VAR_8, 200, 1024);

    }



    



    if (VAR_2) {

        

        if (!stream->is_feed) {

            sprintf(VAR_9, "POST command not handled");

            goto send_error;

        }

        if (http_start_receive_data(VAR_0) < 0) {

            sprintf(VAR_9, "could not open feed");

            goto send_error;

        }

        VAR_0->http_error = 0;

        VAR_0->state = HTTPSTATE_RECEIVE_DATA;

        return 0;

    }



    if (VAR_0->stream->stream_type == STREAM_TYPE_STATUS)

        goto send_stats;



    

    if (open_input_stream(VAR_0, VAR_4) < 0) {

        sprintf(VAR_9, "Input stream corresponding to '%s' not found", VAR_6);

        goto send_error;

    }



    

    VAR_7 = VAR_0->buffer;

    VAR_7 += sprintf(VAR_7, "HTTP/1.0 200 OK\r\n");

    VAR_10 = VAR_0->stream->fmt->VAR_10;

    if (!VAR_10)

        VAR_10 = "application/x-octet_stream";

    VAR_7 += sprintf(VAR_7, "Pragma: no-cache\r\n");



    

    if (!strcmp(VAR_0->stream->fmt->name,"asf")) {

        VAR_7 += sprintf(VAR_7, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=1234\r\nPragma: features=\"broadcast\"\r\n");

        VAR_10 = "application/octet-stream";

    }

    VAR_7 += sprintf(VAR_7, "Content-Type: %s\r\n", VAR_10);

    VAR_7 += sprintf(VAR_7, "\r\n");

    

    

    VAR_0->http_error = 0;

    VAR_0->buffer_ptr = VAR_0->buffer;

    VAR_0->buffer_end = VAR_7;

    VAR_0->state = HTTPSTATE_SEND_HEADER;

    return 0;

 send_error:

    VAR_0->http_error = 404;

    VAR_7 = VAR_0->buffer;

    VAR_7 += sprintf(VAR_7, "HTTP/1.0 404 Not Found\r\n");

    VAR_7 += sprintf(VAR_7, "Content-type: %s\r\n", "text/html");

    VAR_7 += sprintf(VAR_7, "\r\n");

    VAR_7 += sprintf(VAR_7, "<HTML>\n");

    VAR_7 += sprintf(VAR_7, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");

    VAR_7 += sprintf(VAR_7, "<BODY>%s</BODY>\n", VAR_9);

    VAR_7 += sprintf(VAR_7, "</HTML>\n");



    

    VAR_0->buffer_ptr = VAR_0->buffer;

    VAR_0->buffer_end = VAR_7;

    VAR_0->state = HTTPSTATE_SEND_HEADER;

    return 0;

 send_stats:

    compute_stats(VAR_0);

    VAR_0->http_error = 200; 



    VAR_0->state = HTTPSTATE_SEND_HEADER;

    return 0;

}

static int FUNC_0(HTTPContext *VAR_0)
{ char *VAR_13; int VAR_2; char VAR_3[32]; char VAR_4[1024], *VAR_5; char VAR_6[1024], *VAR_7; char VAR_8[32]; char VAR_9[1024]; const char *VAR_10; FFStream *stream; VAR_13 = VAR_0->buffer; VAR_7 = VAR_3; while (!isspace(*VAR_13) && *VAR_13 != '\0') { if ((VAR_7 - VAR_3) < sizeof(VAR_3) - 1)
*VAR_7++ = *VAR_13; VAR_13++; } *VAR_7 = '\0'; if (!strcmp(VAR_3, "GET"))
VAR_2 = 0; else if (!strcmp(VAR_3, "POST"))
VAR_2 = 1; else
return -1; while (isspace(*VAR_13)) VAR_13++; VAR_7 = VAR_6; while (!isspace(*VAR_13) && *VAR_13 != '\0') { if ((VAR_7 - VAR_6) < sizeof(VAR_6) - 1)
*VAR_7++ = *VAR_13; VAR_13++; } *VAR_7 = '\0'; while (isspace(*VAR_13)) VAR_13++; VAR_7 = VAR_8; while (!isspace(*VAR_13) && *VAR_13 != '\0') { if ((VAR_7 - VAR_8) < sizeof(VAR_8) - 1)
*VAR_7++ = *VAR_13; VAR_13++; } *VAR_7 = '\0'; if (strcmp(VAR_8, "HTTP/1.0") && strcmp(VAR_8, "HTTP/1.1"))
return -1; VAR_13 = VAR_6; if (*VAR_13 == '/')
VAR_13++; VAR_5 = VAR_13; VAR_13 = strchr(VAR_13, '?'); if (VAR_13) { strcpy(VAR_4, VAR_13); *VAR_13 = '\0'; } else { VAR_4[0] = '\0'; } stream = first_stream; while (stream != NULL) { if (!strcmp(stream->VAR_5, VAR_5))
break; stream = stream->next; } if (stream == NULL) { sprintf(VAR_9, "File '%s' not found", VAR_6); goto send_error; } VAR_0->stream = stream; { char VAR_11[32], VAR_12[32], *VAR_13; time_t ti; VAR_13 = inet_ntoa(VAR_0->from_addr.sin_addr); strcpy(VAR_11, VAR_13); ti = time(NULL); VAR_13 = ctime(&ti); strcpy(VAR_12, VAR_13); VAR_13 = VAR_12 + strlen(VAR_13) - 1; if (*VAR_13 == '\n')
*VAR_13 = '\0'; http_log("%s - - [%s] \"%s %s %s\" %d %d\n",
VAR_11, VAR_12, VAR_3, VAR_6, VAR_8, 200, 1024); } if (VAR_2) { if (!stream->is_feed) { sprintf(VAR_9, "POST command not handled"); goto send_error; } if (http_start_receive_data(VAR_0) < 0) { sprintf(VAR_9, "could not open feed"); goto send_error; } VAR_0->http_error = 0; VAR_0->state = HTTPSTATE_RECEIVE_DATA; return 0; } if (VAR_0->stream->stream_type == STREAM_TYPE_STATUS)
goto send_stats; if (open_input_stream(VAR_0, VAR_4) < 0) { sprintf(VAR_9, "Input stream corresponding to '%s' not found", VAR_6); goto send_error; } VAR_7 = VAR_0->buffer; VAR_7 += sprintf(VAR_7, "HTTP/1.0 200 OK\r\n"); VAR_10 = VAR_0->stream->fmt->VAR_10; if (!VAR_10)
VAR_10 = "application/x-octet_stream"; VAR_7 += sprintf(VAR_7, "Pragma: no-cache\r\n"); if (!strcmp(VAR_0->stream->fmt->name,"asf")) { VAR_7 += sprintf(VAR_7, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=1234\r\nPragma: features=\"broadcast\"\r\n"); VAR_10 = "application/octet-stream"; } VAR_7 += sprintf(VAR_7, "Content-Type: %s\r\n", VAR_10); VAR_7 += sprintf(VAR_7, "\r\n"); VAR_0->http_error = 0; VAR_0->buffer_ptr = VAR_0->buffer; VAR_0->buffer_end = VAR_7; VAR_0->state = HTTPSTATE_SEND_HEADER; return 0; send_error:
VAR_0->http_error = 404; VAR_7 = VAR_0->buffer; VAR_7 += sprintf(VAR_7, "HTTP/1.0 404 Not Found\r\n"); VAR_7 += sprintf(VAR_7, "Content-type: %s\r\n", "text/html"); VAR_7 += sprintf(VAR_7, "\r\n"); VAR_7 += sprintf(VAR_7, "<HTML>\n"); VAR_7 += sprintf(VAR_7, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n"); VAR_7 += sprintf(VAR_7, "<BODY>%s</BODY>\n", VAR_9); VAR_7 += sprintf(VAR_7, "</HTML>\n"); VAR_0->buffer_ptr = VAR_0->buffer; VAR_0->buffer_end = VAR_7; VAR_0->state = HTTPSTATE_SEND_HEADER; return 0; send_stats:
compute_stats(VAR_0); VAR_0->http_error = 200; VAR_0->state = HTTPSTATE_SEND_HEADER; return 0; }