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