"Fossies" - the Fresh Open Source Software Archive

Member "HTTPing-2.9/http.c" (29 Oct 2022, 2119 Bytes) of package /linux/www/HTTPing-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "http.c" see the Fossies "Dox" file reference documentation.

    1 /* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */
    2 
    3 #include <assert.h>
    4 #include <stdio.h>
    5 #include <string.h>
    6 #include <unistd.h>
    7 #ifndef NO_SSL
    8 #include <openssl/ssl.h>
    9 #include "mssl.h"
   10 #endif
   11 
   12 #include "gen.h"
   13 #include "http.h"
   14 #include "io.h"
   15 #include "utils.h"
   16 
   17 int get_HTTP_headers(int socket_h, SSL *ssl_h, char **headers, int *overflow, double timeout)
   18 {
   19     char *term = NULL;
   20     int len_in=0, len=4096;
   21     char *buffer = (char *)malloc(len + 1);
   22     int rc = RC_OK;
   23 
   24     *headers = NULL;
   25 
   26     memset(buffer, 0x00, len);
   27 
   28     for(;;)
   29     {
   30         int rrc = -1;
   31         int now_n = len - len_in;
   32 
   33 #ifndef NO_SSL
   34         if (ssl_h)
   35             rrc = SSL_read(ssl_h, &buffer[len_in], now_n);
   36         else
   37 #endif
   38             rrc = read_to(socket_h, &buffer[len_in], now_n, timeout);
   39 
   40         if (rrc == 0 || rrc == RC_SHORTREAD)    /* socket closed before request was read? */
   41         {
   42             rc = RC_SHORTREAD;
   43             break;
   44         }
   45         else if (rrc < 0)
   46         {
   47             free(buffer);
   48             return rrc;
   49         }
   50 
   51         len_in += rrc;
   52 
   53         assert(len_in >= 0);
   54         assert(len_in <= len);
   55 
   56         buffer[len_in] = 0x00;
   57 
   58         if (strstr(buffer, "\r\n\r\n") != NULL)
   59             break;
   60 
   61         if (len_in >= len)
   62         {
   63             len <<= 1;
   64             buffer = (char *)realloc(buffer, len + 1);
   65         }
   66     }
   67 
   68     *headers = buffer;
   69 
   70     term = strstr(buffer, "\r\n\r\n");
   71     if (term)
   72         *overflow = len_in - (term - buffer + 4);
   73     else
   74         *overflow = 0;
   75 
   76     return rc;
   77 }
   78 
   79 int dumb_get_HTTP_headers(int socket_h, char **headers, double timeout)
   80 {
   81     int len_in=0, len=4096;
   82     char *buffer = (char *)malloc(len);
   83     int rc = RC_OK;
   84 
   85     *headers = NULL;
   86 
   87     for(;;)
   88     {
   89         int rrc = read_to(socket_h, &buffer[len_in], 1, timeout);
   90         if (rrc == 0 || rrc == RC_SHORTREAD)    /* socket closed before request was read? */
   91         {
   92             rc = RC_SHORTREAD;
   93             break;
   94         }
   95         else if (rrc == RC_TIMEOUT)     /* timeout */
   96         {
   97             free(buffer);
   98             return RC_TIMEOUT;
   99         }
  100 
  101         len_in += rrc;
  102 
  103         buffer[len_in] = 0x00;
  104         if (memcmp(&buffer[len_in - 4], "\r\n\r\n", 4) == 0)
  105             break;
  106         if (memcmp(&buffer[len_in - 2], "\n\n", 2) == 0) /* broken proxies */
  107             break;
  108 
  109         if (len_in == (len - 1))
  110         {
  111             len <<= 1;
  112             buffer = (char *)realloc(buffer, len);
  113         }
  114     }
  115 
  116     *headers = buffer;
  117 
  118     return rc;
  119 }