1 n__http__cookie_8h
gullradriel edited this page 2026-10-25 10:46:33 +01:00

include/nilorea/n_http_cookie.h

Server-side cookies: read a Cookie header, build a Set-Cookie value. More...

Structures

Name
struct N_HTTP_SET_COOKIE
what a Set-Cookie should carry

Detailed Description

Server-side cookies: read a Cookie header, build a Set-Cookie value.

Author: Castagnier Mickael

Version: 1.0

Date: 2026

The server half of RFC 6265 (n_cookies.h is the client-side jar). Reading is zero-copy over the Cookie header value a request parser handed out; building refuses anything that would let a value break out of its attribute (CR, LF, ';', ',', spaces, quotes, controls) and enforces the cookie prefixes browsers enforce: a "__Host-" cookie must be Secure, have Path=/ and no Domain, a "__Secure-" one must be Secure, and SameSite=None must be Secure.

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __NILOREA_HTTP_COOKIE_HEADER
#define __NILOREA_HTTP_COOKIE_HEADER

#ifdef __cplusplus
extern "C" {
#endif


#include "nilorea/n_str.h"

#include <stddef.h>

typedef enum {
    N_HTTP_SAMESITE_UNSET = 0,  
    N_HTTP_SAMESITE_LAX = 1,    
    N_HTTP_SAMESITE_STRICT = 2, 
    N_HTTP_SAMESITE_NONE = 3    
} N_HTTP_SAMESITE;

#define N_HTTP_COOKIE_SESSION (-1LL)

typedef struct N_HTTP_SET_COOKIE {
    const char* name;
    const char* value;
    const char* path;
    const char* domain;
    long long max_age;
    int secure;
    int http_only;
    N_HTTP_SAMESITE same_site;
} N_HTTP_SET_COOKIE;

const char* n_http_cookie_get(const char* header, size_t len, const char* name, size_t* value_len);

size_t n_http_cookie_count(const char* header, size_t len, const char* name);

int n_http_set_cookie_build(N_STR** out, const N_HTTP_SET_COOKIE* c);


#ifdef __cplusplus
}
#endif

#endif /* __NILOREA_HTTP_COOKIE_HEADER */