mirror of
https://github.com/sigmasternchen/CShore
synced 2025-03-15 16:18:56 +00:00
begin of cookies
This commit is contained in:
parent
6a5f84e701
commit
7b8d769145
2 changed files with 61 additions and 0 deletions
52
src/cookies.c
Normal file
52
src/cookies.c
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <headers.h>
|
||||||
|
|
||||||
|
#include "cookies.h"
|
||||||
|
|
||||||
|
char* getCookie(ctx_t ctx, const char* key) {
|
||||||
|
char* cookieHeader = headers_get(&ctx.headers, "Cookie");
|
||||||
|
if (cookieHeader == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cookieHeader = strdup(cookieHeader);
|
||||||
|
if (cookieHeader == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char** saveptr = NULL;
|
||||||
|
char* str = cookieHeader;
|
||||||
|
size_t keyLength = strlen(key);
|
||||||
|
|
||||||
|
char* value = NULL;
|
||||||
|
|
||||||
|
while((str = strtok_r(str, ";", saveptr)) != NULL) {
|
||||||
|
if (strncmp(str, key, keyLength) == 0) {
|
||||||
|
str = strtok_r(NULL, "=", saveptr)
|
||||||
|
if (str == NULL) {
|
||||||
|
// illegal cookie definition; ignore
|
||||||
|
} else {
|
||||||
|
value = str;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
str = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != NULL) {
|
||||||
|
value = strdup(value);
|
||||||
|
if (value == NULL) {
|
||||||
|
free(cookieHeader);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(cookieHeader);
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
free(cookieHeader);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
9
src/cookies.h
Normal file
9
src/cookies.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef COOKIES_H_
|
||||||
|
#define COOKIES_H_
|
||||||
|
|
||||||
|
#include "request.h"
|
||||||
|
|
||||||
|
char* getCookie(ctx_t, const char*);
|
||||||
|
void setCookie(ctx_t, const char*, const char*);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue