#ifndef CGI_H
#define CGI_H
#include "../public/nsapi.h"
#include <cx/buffer.h>
#ifdef __cplusplus
extern "C" {
#endif
enum CgiIOResult {
CGI_IO_COMPLETE =
0,
CGI_IO_NEED_READ,
CGI_IO_NEED_WRITE,
CGI_IO_ERROR
};
typedef enum CgiIOResult CgiIOResult;
typedef struct {
int in[
2];
int out[
2];
int err[
2];
pid_t pid;
} CGIProcess;
typedef struct {
Session *sn;
Request *rq;
CxBuffer tmp;
int status;
char *msg;
size_t response_length;
WSBool cgiheader;
} CGIResponseParser;
typedef struct CGIHandler {
CGIProcess process;
CGIResponseParser *parser;
HttpResponseWriter *response;
char *path;
Event *readev;
Event *writeev;
char *stderr_tmp;
int stderr_tmplen;
int stderr_tmpalloc;
char *writebuf;
size_t writebuf_alloc;
size_t writebuf_size;
size_t writebuf_pos;
size_t count_write;
WSBool poll_out;
WSBool wait_read;
WSBool cgi_eof;
int cgi_kill;
WSBool debug_finished;
int events;
int result;
} CGIHandler;
int send_cgi(pblock *pb, Session *sn, Request *rq);
char** cgi_add_vars(
char **env, Session *sn, Request *rq);
int cgi_start(CGIProcess *p,
char *path,
char *
const argv[],
char *
const envp[]);
int cgi_close(CGIProcess *p);
int cgi_stdout_readevent(EventHandler *ev, Event *event);
int cgi_stderr_readevent(EventHandler *ev, Event *event);
int cgi_event_finish(EventHandler *ev, Event *event);
int cgi_writeevent(EventHandler *ev, Event *event);
CgiIOResult cgi_read_output(CGIHandler *handler, EventHandler *ev,
const char *debug_log);
CGIResponseParser* cgi_parser_new(Session *sn, Request *rq);
void cgi_parser_free(CGIResponseParser *parser);
int cgi_parse_response(CGIResponseParser *parser,
char *buf,
size_t len,
size_t *bpos);
#ifdef __cplusplus
}
#endif
#endif