"Fossies" - the Fresh Open Source Software Archive

Member "HTTPing-2.9/cookies.c" (29 Oct 2022, 2503 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 "cookies.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdlib.h>
    2 #include <string.h>
    3 #include <unistd.h>
    4 
    5 #include "utils.h"
    6 
    7 void add_cookie(char ***cookies, int *n_cookies, char *in)
    8 {
    9     char *in_copy = strdup(in), *is = strchr(in_copy, '=');
   10     int index = 0, found_at = -1;
   11 
   12     if (is)
   13         *is = 0x00;
   14 
   15     for(index=0; index<*n_cookies; index++)
   16     {
   17         char *dummy = strdup((*cookies)[index]);
   18 
   19         is = strchr(dummy, '=');
   20         if (is)
   21             *is = 0x00;
   22 
   23         if (strcmp(in_copy, dummy) == 0)
   24         {
   25             found_at = index;
   26             free(dummy);
   27             break;
   28         }
   29 
   30         free(dummy);
   31     }
   32 
   33     if (found_at >= 0)
   34     {
   35         free((*cookies)[found_at]);
   36 
   37         (*cookies)[found_at] = strdup(in);
   38     }
   39     else
   40     {
   41         *cookies = (char **)realloc(*cookies, (*n_cookies + 1) * sizeof(char *));
   42 
   43         (*cookies)[*n_cookies] = strdup(in);
   44 
   45         (*n_cookies)++;
   46     }
   47 
   48     free(in_copy);
   49 }
   50 
   51 void combine_cookie_lists(char ***destc, int *n_dest, char **src, int n_src)
   52 {
   53     int loop = 0;
   54 
   55     *destc = (char **)realloc(*destc, (*n_dest + n_src) * sizeof(char *));
   56 
   57     for(loop=0; loop<n_src; loop++)
   58         (*destc)[*n_dest + loop] = strdup(src[loop]);
   59 
   60     (*n_dest) += n_src;
   61 }
   62 
   63 void free_cookies(char **dynamic_cookies, int n_dynamic_cookies)
   64 {
   65     int index = 0;
   66 
   67     for(index=0; index<n_dynamic_cookies; index++)
   68         free(dynamic_cookies[index]);
   69 
   70     free(dynamic_cookies);
   71 }
   72 
   73 void get_cookies(const char *headers, char ***dynamic_cookies, int *n_dynamic_cookies, char ***static_cookies, int *n_static_cookies)
   74 {
   75     int index = 0;
   76     char **header_lines = NULL;
   77     int n_header_lines = 0;
   78 
   79     split_string(headers, "\r\n", &header_lines, &n_header_lines);
   80 
   81     for(index=0; index<n_header_lines; index++)
   82     {
   83         char use_static = 0;
   84         char *result = NULL;
   85         int cparts_index = 0;
   86         char **cparts = NULL;
   87         int n_cparts = 0;
   88 
   89         if (strncmp(header_lines[index], "Set-Cookie:", 11) != 0)
   90             continue;
   91 
   92         split_string(&header_lines[index][12], ";", &cparts, &n_cparts);
   93 
   94         for(cparts_index=0; cparts_index<n_cparts; cparts_index++)
   95         {
   96             char *part = cparts[cparts_index];
   97 
   98             while(*part == ' ')
   99                 part++;
  100 
  101             if (strncmp(part, "expires=", 8) == 0)
  102             {
  103                 use_static = 1;
  104                 continue;
  105             }
  106 
  107             if (strncmp(part, "path=", 5) == 0)
  108                 continue;
  109 
  110             if (strncmp(part, "domain=", 7) == 0)
  111                 continue;
  112 
  113             if (strncmp(part, "HttpOnly", 8) == 0)
  114                 continue;
  115 
  116             str_add(&result, "%s ", part);
  117         }
  118 
  119         free_splitted_string(cparts, n_cparts);
  120 
  121         if (use_static)
  122             add_cookie(static_cookies, n_static_cookies, result);
  123         else
  124             add_cookie(dynamic_cookies, n_dynamic_cookies, result);
  125 
  126         free(result);
  127     }
  128 
  129     free_splitted_string(header_lines, n_header_lines);
  130 }