OwnCode/HttpFetcher/http-fetcher-orig/HttpFetcher.cpp
/*
 * Reads the metadata of an HTTP response.
 * Perhaps a little inefficient, as it reads 1 byte at a time, but
 *  I don't think it's that much of a loss (most headers aren't HUGE).
 * Returns:
 *  # of bytes read on success, or
 *  -1 on error
 */
Reads the metadata of an HTTP response.
Perhaps a little inefficient, as it reads 1 byte at a time, but
I don't think it's that much of a loss (most headers aren't HUGE).
Returns:
# of bytes read on success, or
-1 on error
Reads the metadata of an HTTP response . Perhaps a little inefficient as it reads 1 byte at a time but I don ' t think it ' s that much of a loss ( most headers aren ' t HUGE ) . Returns : # of bytes read on success or - 1 on error
int _http_read_header(int sock, char *headerPtr)
{
    fd_set rfds;
    struct timeval tv;
    int bytesRead = 0, newlines = 0, ret, selectRet;
    while (newlines != 2 && bytesRead != HEADER_BUF_SIZE) {
        FD_ZERO(&rfds);
        FD_SET(sock, &rfds);
        tv.tv_sec = timeout;
        tv.tv_usec = 0;
        if (timeout >= 0)
            selectRet = select(sock + 1, &rfds, NULL, NULL, &tv);
        else        
            selectRet = select(sock + 1, &rfds, NULL, NULL, NULL);
        if (selectRet == 0) {
            errorSource = FETCHER_ERROR;
            http_errno = HF_HEADTIMEOUT;
            errorInt = timeout;
            return -1;
        } else if (selectRet == -1) {
            errorSource = ERRNO;
            return -1;
        }
        ret = read(sock, headerPtr, 1);
        if (ret == -1) {
            errorSource = ERRNO;
            return -1;
        }
        bytesRead++;
        if (*headerPtr == '\r') {       
            headerPtr++;
            continue;
        } else if (*headerPtr == '\n')  
            newlines++;
        else
            newlines = 0;
        headerPtr++;
    }
    headerPtr -= 3;     
    *headerPtr = '\0';
    return bytesRead;
}
int _http_read_header ( int sock , char * headerPtr ) { fd_set rfds ; struct timeval tv ; int bytesRead = 0 , newlines = 0 , ret , selectRet ; while ( newlines != 2 && bytesRead != HEADER_BUF_SIZE ) { FD_ZERO ( & rfds ) ; FD_SET ( sock , & rfds ) ; tv . tv_sec = timeout ; tv . tv_usec = 0 ; if ( timeout >= 0 ) selectRet = select ( sock + 1 , & rfds , NULL , NULL , & tv ) ; else selectRet = select ( sock + 1 , & rfds , NULL , NULL , NULL ) ; if ( selectRet == 0 ) { errorSource = FETCHER_ERROR ; http_errno = HF_HEADTIMEOUT ; errorInt = timeout ; return -1 ; } else if ( selectRet == -1 ) { errorSource = ERRNO ; return -1 ; } ret = read ( sock , headerPtr , 1 ) ; if ( ret == -1 ) { errorSource = ERRNO ; return -1 ; } bytesRead ++ ; if ( * headerPtr == ' \r ' ) { headerPtr ++ ; continue ; } else if ( * headerPtr == ' \n ' ) newlines ++ ; else newlines = 0 ; headerPtr ++ ; } headerPtr -= 3 ; * headerPtr = ' \0 ' ; return bytesRead ; }
Reads the metadata of an HTTP response.
Reads the metadata of an HTTP response .
/* No timeout, can block indefinately */ /* Ignore CR */ /* Basically do nothing special, just don't set newlines
             *  to 0 */ /* LF is the separator */ /* Snip the trailing LF's */