static inline bstring request_determine_method(Request *req) { if(Request_is_json(req)) { return &JSON_METHOD; } else if(Request_is_xml(req)) { return &XML_METHOD; } else { return req->request_method; } }
static inline bstring make_log_message(Request *req, const char *remote_addr, int remote_port, int status, int size) { bstring request_method = NULL; if (Request_is_json(req)) { request_method = &JSON_METHOD; } else if (Request_is_xml(req)) { request_method = &XML_METHOD; } else { request_method = req->request_method; } tns_outbuf outbuf = {.buffer = NULL}; bstring b_temp; check(tns_render_log_start(&outbuf), "Could not initialize buffer"); tns_render_number_prepend(&outbuf, size); tns_render_number_prepend(&outbuf, status); b_temp = bfromcstr(Request_is_json(req) ? "" : bdata(req->version)); tns_render_string_prepend(&outbuf, b_temp); bdestroy(b_temp); tns_render_string_prepend(&outbuf, Request_path(req)); tns_render_string_prepend(&outbuf, request_method); tns_render_number_prepend(&outbuf, (int) time(NULL)); tns_render_number_prepend(&outbuf, remote_port); b_temp = bfromcstr(remote_addr); tns_render_string_prepend(&outbuf, b_temp); bdestroy(b_temp); tns_render_string_prepend(&outbuf, req->target_host->name); tns_render_log_end(&outbuf); // log_data now owns the outbuf buffer bstring log_data = tns_outbuf_to_bstring(&outbuf); bconchar(log_data, '\n'); return log_data; error: return NULL; }
int connection_identify_request(Connection *conn) { int next = CLOSE; if(Request_is_xml(conn->req)) { if(biseq(Request_path(conn->req), &POLICY_XML_REQUEST)) { debug("XML POLICY CONNECTION: %s", bdata(Request_path(conn->req))); conn->type = CONN_TYPE_SOCKET; taskname("XML"); next = SOCKET_REQ; } else { debug("XML MESSAGE"); conn->type = CONN_TYPE_MSG; taskname("MSG"); next = MSG_REQ; } } else if(Request_is_json(conn->req)) { debug("JSON SOCKET MESSAGE"); conn->type = CONN_TYPE_MSG; taskname("MSG"); next = MSG_REQ; } else if(Request_is_websocket(conn->req)) { debug("WEBSOCKET MESSAGE"); conn->type = CONN_TYPE_SOCKET; taskname("WS"); next = WS_REQ; } else if(Request_is_http(conn->req)) { debug("HTTP MESSAGE"); conn->type = CONN_TYPE_HTTP; taskname("HTTP"); next = HTTP_REQ; } else { error_response(conn, 500, "Invalid code branch, tell Zed."); } return next; error: return CLOSE; }
static inline int ident_and_register(Connection *conn, int reg_too) { int next = CLOSE; if(Request_is_xml(conn->req)) { if(biseq(Request_path(conn->req), &POLICY_XML_REQUEST)) { debug("XML POLICY CONNECTION"); conn->type = CONN_TYPE_SOCKET; taskname("XML"); next = SOCKET_REQ; } else { debug("XML MESSAGE"); conn->type = CONN_TYPE_MSG; taskname("MSG"); next = MSG_REQ; } } else if(Request_is_json(conn->req)) { debug("JSON SOCKET MESSAGE"); conn->type = CONN_TYPE_MSG; taskname("MSG"); next = MSG_REQ; } else if(Request_is_http(conn->req)) { debug("HTTP MESSAGE"); conn->type = CONN_TYPE_HTTP; taskname("HTTP"); next = HTTP_REQ; } else { error_response(conn, 500, "Invalid code branch, tell Zed."); } if(reg_too) Register_connect(IOBuf_fd(conn->iob), (void*)conn); return next; error: return CLOSE; }