statsproxy.c
static void *
handleFrontendRequest(void *arg)
{
    proxyclient_t     *clnt = (proxyclient_t *) arg;
    int               sock = clnt->fd;
    callback_t        cb;
    char              servRequest[MAXREQSZ];
    char              uri[MAXREQSZ];
    char              method[MAXREQSZ];
    FILE              *serv;

    if ((serv = fdopen(sock, "a+")) == NULL) {
        proxylog(LOG_ERR, "could not open file pointer to socket (%d)", sock);
        close (sock);
        return NULL;
    }

    clnt->fp = serv;

    int done = FALSE;
    while (!done) {
        memset(servRequest, 0, MAXREQSZ);
        memset(uri, 0, MAXREQSZ);
        memset(method, 0, MAXREQSZ);
        // pull the server request
        if (fgets(servRequest, MAXREQSZ, serv) == NULL) {
            goto end;
        }
        sscanf(servRequest, "%15s %1000s", method, uri);

        if (badMethod(method) || uri == NULL) {
            clntError(clnt, HTTP_BADREQUEST, uri);
            goto end;
        } else {
            setClientType(clnt, method);
            if (clnt->type == HTTP_CLIENT) {
                // line buffer to make more interactive
                setlinebuf(clnt->fp);
            } 
        }

        char *uriStr = uri;
        if (uriStr[0] == '/') {
            // strip leading /
            uriStr++;
        }

        char *decodedUri = uri_decode(uriStr);

        if (strchr(uriStr, '?') != NULL) {
            // get base uri string without params
            *strchr(uriStr, '?') = '\0';
        }

        cb = findCallback(clnt, uriStr);
        if (!cb) {
            free(decodedUri);
            clntError(clnt, HTTP_NOTFOUND, uriStr);
            goto end;
        }
        done = (*cb)(clnt, decodedUri);
        free(decodedUri);
        fflush(clnt->fp);
    }
        
    // close the socket and exit this thread
end:
    fflush(serv);
    fclose(serv);
    free(clnt);
    return NULL;
}
// process inbound telnet or web requests
process inbound telnet or web requests
process inbound telnet or web requests
static void *
handleFrontendRequest(void *arg)
{
    proxyclient_t     *clnt = (proxyclient_t *) arg;
    int               sock = clnt->fd;
    callback_t        cb;
    char              servRequest[MAXREQSZ];
    char              uri[MAXREQSZ];
    char              method[MAXREQSZ];
    FILE              *serv;
    if ((serv = fdopen(sock, "a+")) == NULL) {
        proxylog(LOG_ERR, "could not open file pointer to socket (%d)", sock);
        close (sock);
        return NULL;
    }
    clnt->fp = serv;
    int done = FALSE;
    while (!done) {
        memset(servRequest, 0, MAXREQSZ);
        memset(uri, 0, MAXREQSZ);
        memset(method, 0, MAXREQSZ);
        if (fgets(servRequest, MAXREQSZ, serv) == NULL) {
            goto end;
        }
        sscanf(servRequest, "%15s %1000s", method, uri);
        if (badMethod(method) || uri == NULL) {
            clntError(clnt, HTTP_BADREQUEST, uri);
            goto end;
        } else {
            setClientType(clnt, method);
            if (clnt->type == HTTP_CLIENT) {
                setlinebuf(clnt->fp);
            } 
        }
        char *uriStr = uri;
        if (uriStr[0] == '/') {
            uriStr++;
        }
        char *decodedUri = uri_decode(uriStr);
        if (strchr(uriStr, '?') != NULL) {
            *strchr(uriStr, '?') = '\0';
        }
        cb = findCallback(clnt, uriStr);
        if (!cb) {
            free(decodedUri);
            clntError(clnt, HTTP_NOTFOUND, uriStr);
            goto end;
        }
        done = (*cb)(clnt, decodedUri);
        free(decodedUri);
        fflush(clnt->fp);
    }
end:
    fflush(serv);
    fclose(serv);
    free(clnt);
    return NULL;
}
static void * handleFrontendRequest ( void * arg ) { proxyclient_t * clnt = ( proxyclient_t * ) arg ; int sock = clnt -> fd ; callback_t cb ; char servRequest [ MAXREQSZ ] ; char uri [ MAXREQSZ ] ; char method [ MAXREQSZ ] ; FILE * serv ; if ( ( serv = fdopen ( sock , " " ) ) == NULL ) { proxylog ( LOG_ERR , " " , sock ) ; close ( sock ) ; return NULL ; } clnt -> fp = serv ; int done = FALSE ; while ( ! done ) { memset ( servRequest , 0 , MAXREQSZ ) ; memset ( uri , 0 , MAXREQSZ ) ; memset ( method , 0 , MAXREQSZ ) ; if ( fgets ( servRequest , MAXREQSZ , serv ) == NULL ) { goto end ; } sscanf ( servRequest , " " , method , uri ) ; if ( badMethod ( method ) || uri == NULL ) { clntError ( clnt , HTTP_BADREQUEST , uri ) ; goto end ; } else { setClientType ( clnt , method ) ; if ( clnt -> type == HTTP_CLIENT ) { setlinebuf ( clnt -> fp ) ; } } char * uriStr = uri ; if ( uriStr [ 0 ] == ' ' ) { uriStr ++ ; } char * decodedUri = uri_decode ( uriStr ) ; if ( strchr ( uriStr , ' ' ) != NULL ) { * strchr ( uriStr , ' ' ) = ' \0 ' ; } cb = findCallback ( clnt , uriStr ) ; if ( ! cb ) { free ( decodedUri ) ; clntError ( clnt , HTTP_NOTFOUND , uriStr ) ; goto end ; } done = ( * cb ) ( clnt , decodedUri ) ; free ( decodedUri ) ; fflush ( clnt -> fp ) ; } end : fflush ( serv ) ; fclose ( serv ) ; free ( clnt ) ; return NULL ; }
process inbound telnet or web requests
process inbound telnet or web requests
// pull the server request // line buffer to make more interactive // strip leading / // get base uri string without params // close the socket and exit this thread