qio_channel_websock_extract_headers(char *buffer,

                                    QIOChannelWebsockHTTPHeader *hdrs,

                                    size_t nhdrsalloc,

                                    Error **errp)

{

    char *nl, *sep, *tmp;

    size_t nhdrs = 0;



    /*

     * First parse the HTTP protocol greeting of format:

     *

     *   $METHOD $PATH $VERSION

     *

     * e.g.

     *

     *   GET / HTTP/1.1

     */



    nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

    if (!nl) {

        error_setg(errp, "Missing HTTP header delimiter");

        return 0;

    }

    *nl = '\0';



    tmp = strchr(buffer, ' ');

    if (!tmp) {

        error_setg(errp, "Missing HTTP path delimiter");

        return 0;

    }

    *tmp = '\0';



    if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {

        error_setg(errp, "Unsupported HTTP method %s", buffer);

        return 0;

    }



    buffer = tmp + 1;

    tmp = strchr(buffer, ' ');

    if (!tmp) {

        error_setg(errp, "Missing HTTP version delimiter");

        return 0;

    }

    *tmp = '\0';



    if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) {

        error_setg(errp, "Unexpected HTTP path %s", buffer);

        return 0;

    }



    buffer = tmp + 1;



    if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {

        error_setg(errp, "Unsupported HTTP version %s", buffer);

        return 0;

    }



    buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);



    /*

     * Now parse all the header fields of format

     *

     *   $NAME: $VALUE

     *

     * e.g.

     *

     *   Cache-control: no-cache

     */

    do {

        QIOChannelWebsockHTTPHeader *hdr;



        nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

        if (nl) {

            *nl = '\0';

        }



        sep = strchr(buffer, ':');

        if (!sep) {

            error_setg(errp, "Malformed HTTP header");

            return 0;

        }

        *sep = '\0';

        sep++;

        while (*sep == ' ') {

            sep++;

        }



        if (nhdrs >= nhdrsalloc) {

            error_setg(errp, "Too many HTTP headers");

            return 0;

        }



        hdr = &hdrs[nhdrs++];

        hdr->name = buffer;

        hdr->value = sep;



        /* Canonicalize header name for easier identification later */

        for (tmp = hdr->name; *tmp; tmp++) {

            *tmp = g_ascii_tolower(*tmp);

        }



        if (nl) {

            buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

        }

    } while (nl != NULL);



    return nhdrs;

}

qio_channel_websock_extract_headers(char *buffer,

                                    QIOChannelWebsockHTTPHeader *hdrs,

                                    size_t nhdrsalloc,

                                    Error **errp)

{

    char *nl, *sep, *tmp;

    size_t nhdrs = 0;



    



















    nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

    if (!nl) {

        error_setg(errp, "Missing HTTP header delimiter");

        return 0;

    }

    *nl = '\0';



    tmp = strchr(buffer, ' ');

    if (!tmp) {

        error_setg(errp, "Missing HTTP path delimiter");

        return 0;

    }

    *tmp = '\0';



    if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {

        error_setg(errp, "Unsupported HTTP method %s", buffer);

        return 0;

    }



    buffer = tmp + 1;

    tmp = strchr(buffer, ' ');

    if (!tmp) {

        error_setg(errp, "Missing HTTP version delimiter");

        return 0;

    }

    *tmp = '\0';



    if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) {

        error_setg(errp, "Unexpected HTTP path %s", buffer);

        return 0;

    }



    buffer = tmp + 1;



    if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {

        error_setg(errp, "Unsupported HTTP version %s", buffer);

        return 0;

    }



    buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);



    

















    do {

        QIOChannelWebsockHTTPHeader *hdr;



        nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

        if (nl) {

            *nl = '\0';

        }



        sep = strchr(buffer, ':');

        if (!sep) {

            error_setg(errp, "Malformed HTTP header");

            return 0;

        }

        *sep = '\0';

        sep++;

        while (*sep == ' ') {

            sep++;

        }



        if (nhdrs >= nhdrsalloc) {

            error_setg(errp, "Too many HTTP headers");

            return 0;

        }



        hdr = &hdrs[nhdrs++];

        hdr->name = buffer;

        hdr->value = sep;



        

        for (tmp = hdr->name; *tmp; tmp++) {

            *tmp = g_ascii_tolower(*tmp);

        }



        if (nl) {

            buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

        }

    } while (nl != NULL);



    return nhdrs;

}

qio_channel_websock_extract_headers(char *buffer,         return 0;         return 0;         return 0;         return 0;             return 0;             return 0;
FUNC_0(char *VAR_0,

                                    QIOChannelWebsockHTTPHeader *VAR_1,

                                    size_t VAR_2,

                                    Error **VAR_3)

{

    char *VAR_4, *VAR_5, *VAR_6;

    size_t nhdrs = 0;



    



















    VAR_4 = strstr(VAR_0, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

    if (!VAR_4) {

        error_setg(VAR_3, "Missing HTTP header delimiter");

        return 0;

    }

    *VAR_4 = '\0';



    VAR_6 = strchr(VAR_0, ' ');

    if (!VAR_6) {

        error_setg(VAR_3, "Missing HTTP path delimiter");

        return 0;

    }

    *VAR_6 = '\0';



    if (!g_str_equal(VAR_0, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {

        error_setg(VAR_3, "Unsupported HTTP method %s", VAR_0);

        return 0;

    }



    VAR_0 = VAR_6 + 1;

    VAR_6 = strchr(VAR_0, ' ');

    if (!VAR_6) {

        error_setg(VAR_3, "Missing HTTP version delimiter");

        return 0;

    }

    *VAR_6 = '\0';



    if (!g_str_equal(VAR_0, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) {

        error_setg(VAR_3, "Unexpected HTTP path %s", VAR_0);

        return 0;

    }



    VAR_0 = VAR_6 + 1;



    if (!g_str_equal(VAR_0, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {

        error_setg(VAR_3, "Unsupported HTTP version %s", VAR_0);

        return 0;

    }



    VAR_0 = VAR_4 + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);



    

















    do {

        QIOChannelWebsockHTTPHeader *hdr;



        VAR_4 = strstr(VAR_0, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

        if (VAR_4) {

            *VAR_4 = '\0';

        }



        VAR_5 = strchr(VAR_0, ':');

        if (!VAR_5) {

            error_setg(VAR_3, "Malformed HTTP header");

            return 0;

        }

        *VAR_5 = '\0';

        VAR_5++;

        while (*VAR_5 == ' ') {

            VAR_5++;

        }



        if (nhdrs >= VAR_2) {

            error_setg(VAR_3, "Too many HTTP headers");

            return 0;

        }



        hdr = &VAR_1[nhdrs++];

        hdr->name = VAR_0;

        hdr->value = VAR_5;



        

        for (VAR_6 = hdr->name; *VAR_6; VAR_6++) {

            *VAR_6 = g_ascii_tolower(*VAR_6);

        }



        if (VAR_4) {

            VAR_0 = VAR_4 + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);

        }

    } while (VAR_4 != NULL);



    return nhdrs;

}

FUNC_0(char *VAR_0,
QIOChannelWebsockHTTPHeader *VAR_1,
size_t VAR_2,
Error **VAR_3)
{ char *VAR_4, *VAR_5, *VAR_6; size_t nhdrs = 0; VAR_4 = strstr(VAR_0, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM); if (!VAR_4) { error_setg(VAR_3, "Missing HTTP header delimiter"); return 0; } *VAR_4 = '\0'; VAR_6 = strchr(VAR_0, ' '); if (!VAR_6) { error_setg(VAR_3, "Missing HTTP path delimiter"); return 0; } *VAR_6 = '\0'; if (!g_str_equal(VAR_0, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) { error_setg(VAR_3, "Unsupported HTTP method %s", VAR_0); return 0; } VAR_0 = VAR_6 + 1; VAR_6 = strchr(VAR_0, ' '); if (!VAR_6) { error_setg(VAR_3, "Missing HTTP version delimiter"); return 0; } *VAR_6 = '\0'; if (!g_str_equal(VAR_0, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) { error_setg(VAR_3, "Unexpected HTTP path %s", VAR_0); return 0; } VAR_0 = VAR_6 + 1; if (!g_str_equal(VAR_0, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) { error_setg(VAR_3, "Unsupported HTTP version %s", VAR_0); return 0; } VAR_0 = VAR_4 + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM); do { QIOChannelWebsockHTTPHeader *hdr; VAR_4 = strstr(VAR_0, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM); if (VAR_4) { *VAR_4 = '\0'; } VAR_5 = strchr(VAR_0, ':'); if (!VAR_5) { error_setg(VAR_3, "Malformed HTTP header"); return 0; } *VAR_5 = '\0'; VAR_5++; while (*VAR_5 == ' ') { VAR_5++; } if (nhdrs >= VAR_2) { error_setg(VAR_3, "Too many HTTP headers"); return 0; } hdr = &VAR_1[nhdrs++]; hdr->name = VAR_0; hdr->value = VAR_5; for (VAR_6 = hdr->name; *VAR_6; VAR_6++) { *VAR_6 = g_ascii_tolower(*VAR_6); } if (VAR_4) { VAR_0 = VAR_4 + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM); } } while (VAR_4 != NULL); return nhdrs; }