view seobeo/tests/seobeo_request_context_test.c @ 269:de291f396881

install initial production config Copy the ignored repository config into /etc/mrjunejune on first deployment while preserving existing production configuration on later deploys. Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Fri, 07 Aug 2026 13:08:10 -0700
parents 04fee26ecce0
children
line wrap: on
line source

#include "seobeo/seobeo.h"
#include "seobeo/seobeo_internal.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

static const char *request_value(
    Seobeo_Request_Entry *request,
    const char *key)
{
  void *entry = Dowa_HashMap_Get_Ptr(request, key);
  return entry ? ((Seobeo_Request_Entry *)entry)->value : NULL;
}

int main(void)
{
  int sockets[2];
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0)
    return 1;

  const char raw_request[] =
      "GET / HTTP/1.1\r\n"
      "Host: example.test\r\n"
      "cookie: mjj_session=session-token\r\n"
      "origin: https://example.test\r\n"
      "x-csrf-token: csrf-token\r\n"
      "Remote-Addr: 198.51.100.99\r\n"
      "X-Real-IP: 203.0.113.45\r\n"
      "\r\n";
  if (write(sockets[1], raw_request, sizeof(raw_request) - 1) < 0)
    return 1;

  Seobeo_Handle handle = {0};
  handle.socket = sockets[0];
  handle.host = strdup("2001:db8::1234");
  handle.read_buffer_capacity = 4096;
  handle.read_buffer = malloc(handle.read_buffer_capacity);

  Dowa_Arena *arena = Dowa_Arena_Create(16 * 1024);
  Seobeo_Request_Entry *request = NULL;
  int failed = Seobeo_Web_Header_Parse(&handle, &request, arena) != 0;

  const char *remote_addr = request_value(request, "Remote-Addr");
  const char *forwarded_addr = request_value(request, "X-Real-IP");
  const char *cookie = request_value(request, "Cookie");
  const char *origin = request_value(request, "Origin");
  const char *csrf = request_value(request, "X-CSRF-Token");
  if (!remote_addr || strcmp(remote_addr, "2001:db8::1234") != 0)
    failed = 1;
  if (!forwarded_addr || strcmp(forwarded_addr, "203.0.113.45") != 0)
    failed = 1;
  if (!cookie || strcmp(cookie, "mjj_session=session-token") != 0)
    failed = 1;
  if (!origin || strcmp(origin, "https://example.test") != 0)
    failed = 1;
  if (!csrf || strcmp(csrf, "csrf-token") != 0)
    failed = 1;
  if (remote_addr == handle.host)
    failed = 1;

  if (failed)
    fprintf(stderr, "Request context did not preserve peer identity or canonical headers\n");

  close(sockets[0]);
  close(sockets[1]);
  free(handle.host);
  free(handle.read_buffer);
  Dowa_Arena_Free(arena);
  return failed;
}