/* Small kcgi helpers shared by signet-api and pim-api. */
#ifndef SIGNET_HTTP_H
#define SIGNET_HTTP_H

#include <sys/types.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <kcgi.h>

#include "conf.h"

struct ident {
	const char *user;	/* NULL when the proxy sent no identity */
	const char *groups;
};

void ident_read(struct kreq *, const struct conf *, struct ident *);
int ident_has_role(const struct ident *, const struct conf *, const char *role);

const char *http_header(struct kreq *, const char *name);
int http_keep_fields(struct kreq *, const char *const *, size_t);
struct kpair *http_field(struct kreq *, const char *key);
const char *http_field_str(struct kreq *, const char *key);
/* state-changing requests must carry X-Requested-With: signet (CSRF guard) */
int http_post_ok(struct kreq *);
/* split r->fullpath into at most `max` segments (modifies `buf`) */
int http_path(struct kreq *, char *buf, size_t bufsz, char **seg, int max);

void send_json(struct kreq *, enum khttp, const char *body);
/* API loops lend r->arg their configuration; log the site, not error contents. */
/* APIs lend their conf through r->arg after parsing, for failure identity. */
void send_err_at(struct kreq *, enum khttp, const char *, const char *, int);
#define send_err(r, code, msg) send_err_at((r), (code), (msg), __func__, __LINE__)
void send_file(struct kreq *, const char *path, const char *mime,
    const char *download_name);
const char *http_method_str(enum kmethod);

#endif
