31 #define UNUSED __attribute__ ((unused))
51 bzero(&state->
db,
sizeof(state->
db));
57 state->
path[0] =
'\0';
60 state->
body[0] =
'\0';
66 log(LOG_CONNECTION_INFO,
"Not implemented, assuming connection is already accepted");
70 if (conn->
ssl == NULL) {
76 log(LOG_CONNECTION_INFO,
"Accepted SSL connection on fd %d", conn->
fd);
78 }
else if (rtn == -1) {
82 log(LOG_CONNECTION_INFO,
"SSL accept incomplete (fd: %d)", conn->
fd);
88 int use_ssl = (state->
conn.
ssl != NULL);
101 log(LOG_CONNECTION_INFO,
"Read incomplete (fd: %d)", state->
conn.
fd);
105 log(LOG_CONNECTION_INFO,
"Completed reading %d bytes (fd: %d)",
109 log(LOG_WS_ERRORS,
"Error reading (fd: %d)", state->
conn.
fd);
122 log(LOG_CONNECTION_INFO,
"Request complete (fd: %d)", state->
conn.
fd);
125 log(LOG_CONNECTION_INFO,
"Partial request received (fd: %d)", state->
conn.
fd);
131 log_error(
"Unknown status %d returned from parrser (fd: %d)", status, state->
conn.
fd);
136 #define REGEX_KEY "regex="
139 char *regex_loc = strstr(url,
REGEX_KEY);
140 if (regex_loc == NULL) {
141 log(LOG_CONNECTION_INFO,
"Found no %s in %s",
REGEX_KEY, url);
144 log(LOG_CONNECTION_INFO,
"Found regex");
148 #define MAX_REGEX_VALUE_LEN 64
151 char *regex_start = strstr(url,
REGEX_KEY);
152 if (regex_start == NULL)
155 int start_i = (regex_start - url);
162 strncpy(regex, &url[start_i], i - start_i);
163 regex[i-start_i] =
'\0';
169 log_error(
"Requested regex value (%s) too long", regex_start);
174 if ( strstr(url,
"database") == NULL ) {
180 log(LOG_CONNECTION_INFO,
"Successfully queried db");
184 log(LOG_CONNECTION_INFO,
"Partial DB query");
190 log_error(
"Unknown return %d from database query", rtn);
195 #define DEFAULT_HTTP_HEADER\
196 "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n" \
198 #define DEFAULT_HTTP_BODY\
199 "<!DOCTYPE html>\n<html>\n<body>\n<h1>Dedos New Runtime</h1>\n</body>\n</html>"
201 #define ERROR_HTTP_HEADER\
202 "HTTP/1.1 418 IM A TEAPOT\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n"
204 #define ERROR_HTTP_BODY\
205 "<!DOCTYPE html>\n<html>\n<body>\n<h1>418 I am a teapot</h1>\n</body>\n</html>"
209 (
int)strlen(ERROR_HTTP_BODY));
215 (
int)strlen(DEFAULT_HTTP_BODY));
216 log(LOG_CONNECTION_INFO,
"Crafted non-regex response");
224 log_error(
"Non-regex URL passed to craft_regex_response!");
230 log_error(
"Error crafting regex response");
234 log(LOG_CONNECTION_INFO,
"Crafted regex response");
235 return strlen(response);
240 if (state->
body[0] ==
'\0' && state->
header[0] ==
'\0') {
241 log_error(
"Attempted to write empty response");
245 int use_ssl = (state->
conn.
ssl != NULL);
257 log(LOG_CONNECTION_INFO,
"Header write incomplete (fd: %d)", state->
conn.
fd);
260 log(LOG_CONNECTION_INFO,
"Completed writing headers (fd: %d)", state->
conn.
fd);
266 log_error(
"Unknown return %d from call to write (fd: %d)", rtn, state->
conn.
fd);
281 log(LOG_CONNECTION_INFO,
"Write incomplete (fd: %d)", state->
conn.
fd);
284 log(LOG_CONNECTION_INFO,
"Completed writing response (fd: %d)", state->
conn.
fd);
290 log_error(
"Unknown return %d from call to write (fd: %d)", rtn, state->
conn.
fd);
299 log(LOG_CONNECTION_INFO,
"Closing connection (fd: %d)", conn->
fd);
300 if (conn->
ssl != NULL) {
304 int rtn = close(conn->
fd);
306 log_perror(
"Error closing connection (fd: %d)", conn->
fd);
309 log(LOG_CONNECTION_INFO,
"Closed connection (fd: %d)", conn->
fd);
void init_http_state(struct http_state *state, struct connection *conn)
int regex_html(char *to_match, char *htmlDoc)
struct parser_state parser
int parse_http(struct parser_state *state, char *buf, ssize_t bytes)
void init_connection(struct connection *conn, int fd)
int write_socket(int fd, char *buf, int *buf_size)
SSL * init_ssl_connection(int fd)
int read_ssl(SSL *ssl, char *buf, int *buf_size)
int craft_error_response(char *url, char *response)
int craft_regex_response(char *url, char *response)
#define WS_INCOMPLETE_READ
#define log_perror(fmt,...)
#define DEFAULT_HTTP_BODY
Logging of status messages to the terminal.
int write_response(struct response_state *state)
#define MAX_REGEX_VALUE_LEN
#define WS_INCOMPLETE_WRITE
int close_connection(struct connection *conn)
static int get_regex_value(char *url, char *regex)
void init_read_state(struct read_state *state, struct connection *conn)
int accept_connection(struct connection *conn, int use_ssl)
#define log_error(fmt,...)
int parse_request(char *req, int req_len, struct http_state *state)
int read_socket(int fd, char *buf, int *buf_size)
#define DEFAULT_HTTP_HEADER
int access_database(char *url, struct db_state *state)
void init_parser_state(struct parser_state *state)
int query_db(struct db_state *state)
int craft_nonregex_response(char *url, char *response)
int write_ssl(SSL *ssl, char *buf, int *buf_size)
#define log(level, fmt,...)
Log at a custom level.
void init_response_state(struct response_state *state, struct connection *conn)
int read_request(struct read_state *state)
#define ERROR_HTTP_HEADER
enum webserver_status status