File: zhttpd.h

package info (click to toggle)
libzia 4.36-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,320 kB
  • sloc: ansic: 24,172; sh: 4,408; makefile: 211
file content (122 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
    zhttpd.h - Simple HTTP daemon
    Copyright (C) 2011-2020 Ladislav Vaiz <ok1zia@nagano.cz>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    version 2 as published by the Free Software Foundation.

*/

#ifndef __ZHTTPD_H
#define __ZHTTPD_H

#define ZWS_CONTINUE 0x00 
#define ZWS_TEXT     0x01
#define ZWS_BINARY   0x02
#define ZWS_CLOSE    0x08
#define ZWS_PING     0x09
#define ZWS_PONG     0x0a


#include <zasyncdns.h>
#include <zbinbuf.h>
#include <zselect.h>
#include <zthread.h>
#include <glib.h>

struct zhttpconn;

struct zhttpd{
	struct zselect *zsel;
    int port;
    int sock;

	GPtrArray *bindings; // of struct zhttpdbinding
    GPtrArray *conns;
	GMutex *conns_mutex;
	void(*conn_new)(struct zhttpconn *conn);
	void(*conn_close)(struct zhttpconn *conn);
};

struct zhttpheader{
	char *key, *value;
};

struct zhttpconn{
	struct zhttpd *zhttpd;
    int sock;
    GString *request;

	MUTEX_DEFINE(response);
	int response_i;
	struct zbinbuf *response;

    
	struct sockaddr_in peer;
	GPtrArray *response_headers;
	int status;

	char *req_path;
	char *req_args;
    char *req_data;
	GPtrArray *memlist; 
	struct zhttpdbinding *binding; 

	int is_ws;
    struct zbinbuf *wsbuf;
	int ws_ping_timer_id;

	int is_stream;
	void *user_data;
};

struct zhttpdbinding{
	struct zhttpd *zhttpd;
	GRegex *regex;
	void (*handler)(struct zhttpconn *conn);

	// for file binding
	char *file_docroot;

	// for websockets binding
	void (*ws_onmessage)(struct zhttpconn *conn, int opcode, char *buf0, int len);

};

struct zhttpd *zhttpd_init(struct zselect *zsel, int port, int loopback);
void zhttpd_handlers(struct zhttpd *httpd, void(*conn_new)(struct zhttpconn *conn), void(*conn_close)(struct zhttpconn *conn));
void zhttpd_free(struct zhttpd *zhttpd);

void zhttpd_free_conn(struct zhttpconn *conn);
void zhttpd_close_conn(struct zhttpconn *conn);
void zhttpd_free_binding(struct zhttpdbinding *b);

void zhttpd_free_header(struct zhttpheader *header);

void zhttpd_accept_handler(void *arg);
void zhttpd_read_handler(void *arg);
void zhttpd_write_handler(void *arg);
//int zhttpd_flush(struct zhttpconn *conn);
void zhttpd_write_response_header(struct zhttpconn *conn);
void zhttpd_response(struct zhttpconn *conn, int status, char *contenttype);
void zhttpd_add_header(struct zhttpconn *conn, char *header, char *value);
void zhttpd_get(struct zhttpconn *conn);
void zhttpd_post(struct zhttpconn *conn);

struct zhttpdbinding *zhttpd_add_binding(struct zhttpd *zhttpd, char *regex, 
	void (*handler)(struct zhttpconn *conn));

char *zhttpd_arg(struct zhttpconn *conn, char *key, char *def);
int zhttpd_arg_int(struct zhttpconn *conn, char *key, int def);

void zhttpd_file_handler(struct zhttpconn *conn);
void zhttpd_ws_handshake_handler(struct zhttpconn *conn);
void zhttpd_ws_read_handler(void *arg);
char *zhttpd_get_header(struct zhttpconn *conn, char *header, char *def);
void zhttpd_ws_send(struct zhttpconn *conn, int opcode, char *s, int len);
void zhttpd_ws_send_all(struct zhttpd *zhttpd, int opcode, char *s, int len);
void zhttpd_ws_ping_timer(void *arg);
void zhttpd_write(struct zhttpconn *conn, const void *data, int len);
int http_response_buf_len(struct zhttpconn *conn);
#endif