static ret_t digest_HA2 (cherokee_validator_t *validator, cherokee_buffer_t *buf, cherokee_connection_t *conn) { ret_t ret; const char *method; cuint_t method_len; /* Sanity check */ if (cherokee_buffer_is_empty (&validator->uri)) return ret_deny; ret = cherokee_http_method_to_string (conn->header.method, &method, &method_len); if (unlikely (ret != ret_ok)) return ret; cherokee_buffer_ensure_size (buf, method_len + 1 + validator->uri.len); cherokee_buffer_add (buf, method, method_len); cherokee_buffer_add_str (buf, ":"); cherokee_buffer_add_buffer (buf, &validator->uri); cherokee_buffer_encode_md5_digest (buf); return ret_ok; }
/* Utilities */ ret_t cherokee_handler_add_header_options (cherokee_handler_t *hdl, cherokee_buffer_t *buffer) { ret_t ret; int http_n; const char *method_name; cuint_t method_name_len; cherokee_http_method_t method; cherokee_http_method_t supported_methods; cherokee_boolean_t first = true; /* Introspect the handler */ supported_methods = PLUGIN_INFO_HANDLER (MODULE (hdl)->info)->valid_methods; /* Build the response string */ cherokee_buffer_add_str (buffer, "Allow: "); for (http_n=0; http_n < cherokee_http_method_LENGTH; http_n++) { method = HTTP_METHOD (1LL << http_n); if (supported_methods & method) { method_name = NULL; method_name_len = 0; if (! first) { cherokee_buffer_add_str (buffer, ", "); } else { first = false; } ret = cherokee_http_method_to_string (method, &method_name, &method_name_len); if (unlikely ((ret != ret_ok) || (! method_name))) { continue; } cherokee_buffer_add (buffer, method_name, method_name_len); } } cherokee_buffer_add_str (buffer, CRLF); return ret_ok; }
ret_t cherokee_request_header_build_string (cherokee_request_header_t *request, cherokee_buffer_t *buf, cherokee_buffer_t *tmp1, cherokee_buffer_t *tmp2) { ret_t ret; const char *ptr; cuint_t len; cherokee_url_t *url = REQUEST_URL(request); /* 200 bytes should be enough for a small header */ cherokee_buffer_ensure_size (buf, 200); /* Add main request line: * GET /dir/object HTTP/1.1 */ ret = cherokee_http_method_to_string (request->method, &ptr, &len); if (ret != ret_ok) return ret; ret = cherokee_buffer_add (buf, ptr, len); if (ret != ret_ok) return ret; cherokee_buffer_add_str (buf, " "); /* Check if the requests goes through a proxy */ if (request->proxy) { cherokee_buffer_add_str (buf, "http://"); cherokee_buffer_add_buffer (buf, URL_HOST(url)); } cherokee_buffer_add_buffer (buf, URL_REQUEST(url)); switch (REQUEST_VERSION(request)) { case http_version_11: cherokee_buffer_add_str (buf, " HTTP/1.1" CRLF); break; case http_version_10: cherokee_buffer_add_str (buf, " HTTP/1.0" CRLF); break; default: SHOULDNT_HAPPEN; return ret_error; } /* Add "Host:" header - in HTTP/1.1 */ if (REQUEST_VERSION(request) == http_version_11) { cherokee_buffer_add_str (buf, "Host: "); cherokee_buffer_add_buffer (buf, URL_HOST(url)); cherokee_buffer_add_str (buf, CRLF); } /* Post information */ if (request->post_len != 0) { /* "Content-Length: " FMT_OFFSET CRLF, request->post_len; */ cherokee_buffer_add_str (buf, "Content-Length: "); cherokee_buffer_add_ullong10 (buf, (cullong_t) request->post_len); cherokee_buffer_add_str (buf, CRLF); } /* Add "Connection:" header */ if (REQUEST_KEEPALIVE(request)) { cherokee_buffer_add_str (buf, "Connection: Keep-Alive"CRLF); } else { cherokee_buffer_add_str (buf, "Connection: close"CRLF); } /* Authentication */ if (! cherokee_buffer_is_empty (&request->user) || ! cherokee_buffer_is_empty (&request->password)) { cherokee_buffer_clean (tmp1); cherokee_buffer_clean (tmp2); cherokee_buffer_add_buffer (tmp1, &request->user); cherokee_buffer_add_char (tmp1, ':'); cherokee_buffer_add_buffer (tmp1, &request->password); cherokee_buffer_encode_base64 (tmp1, tmp2); cherokee_buffer_add_str (buf, "Authorization: Basic "); cherokee_buffer_add_buffer (buf, tmp2); cherokee_buffer_add_str (buf, CRLF); } /* Extra headers */ if (! cherokee_buffer_is_empty (&request->extra_headers)) { cherokee_buffer_add_buffer (buf, &request->extra_headers); } /* Finish the header */ cherokee_buffer_add_str (buf, CRLF); return ret_ok; }
static ret_t build_log_string (cherokee_logger_ncsa_t *logger, cherokee_connection_t *cnt, cherokee_buffer_t *buf) { ret_t ret; const char *method; const char *username; const char *version; cuint_t method_len = 0; size_t username_len = 0; cuint_t version_len = 0; cherokee_buffer_t *referer = &logger->referer; cherokee_buffer_t *useragent = &logger->useragent; char ipaddr[CHE_INET_ADDRSTRLEN]; /* Look for the user */ if (cnt->validator && !cherokee_buffer_is_empty (&cnt->validator->user)) { username_len = cnt->validator->user.len; username = cnt->validator->user.buf; } else { username_len = 1; username = "******"; } /* Get the method and version strings */ ret = cherokee_http_method_to_string (cnt->header.method, &method, &method_len); if (unlikely (ret < ret_ok)) { method = ""; method_len = 0; } ret = cherokee_http_version_to_string (cnt->header.version, &version, &version_len); if (unlikely (ret < ret_ok)) { version = ""; version_len = 0; } /* Build the log string * * "%s - %s [%02d/%s/%d:%02d:%02d:%02d %c%02d%02d] \"%s %s %s\" %d " * FMT_OFFSET */ if (cherokee_buffer_is_empty (&cnt->logger_real_ip)) { memset (ipaddr, 0, sizeof(ipaddr)); cherokee_socket_ntop (&cnt->socket, ipaddr, sizeof(ipaddr)-1); cherokee_buffer_add (buf, ipaddr, strlen(ipaddr)); } else { cherokee_buffer_add_buffer (buf, &cnt->logger_real_ip); } cherokee_buffer_add_str (buf, " - "); cherokee_buffer_add (buf, username, username_len); /* " [date time] " */ cherokee_buffer_add_buffer (buf, &now); cherokee_buffer_add (buf, method, method_len); cherokee_buffer_add_char (buf, ' '); if (! cherokee_buffer_is_empty (&cnt->request_original)) { cherokee_buffer_add_buffer (buf, &cnt->request_original); if (! cherokee_buffer_is_empty (&cnt->query_string_original)) { cherokee_buffer_add_char (buf, '?'); cherokee_buffer_add_buffer (buf, &cnt->query_string_original); } } else { cherokee_buffer_add_buffer (buf, &cnt->request); if (! cherokee_buffer_is_empty (&cnt->query_string)) { cherokee_buffer_add_char (buf, '?'); cherokee_buffer_add_buffer (buf, &cnt->query_string); } } cherokee_buffer_add_char (buf, ' '); cherokee_buffer_add (buf, version, version_len); cherokee_buffer_add_str (buf, "\" "); if (unlikely (cnt->error_internal_code != http_unset)) { cherokee_buffer_add_long10 (buf, cnt->error_internal_code); } else { cherokee_buffer_add_long10 (buf, cnt->error_code); } cherokee_buffer_add_char (buf, ' '); cherokee_buffer_add_ullong10 (buf, cnt->tx); /* Look for the "combined" information */ if (!logger->combined) { cherokee_buffer_add_char (buf, '\n'); return ret_ok; } /* "combined" information */ cherokee_buffer_clean (referer); cherokee_buffer_clean (useragent); cherokee_header_copy_known (&cnt->header, header_referer, referer); cherokee_header_copy_known (&cnt->header, header_user_agent, useragent); cherokee_buffer_ensure_addlen (buf, 8 + referer->len + referer->len); if (referer->len > 0) { cherokee_buffer_add_str (buf, " \""); cherokee_buffer_add_buffer (buf, referer); cherokee_buffer_add_str (buf, "\" \""); } else { cherokee_buffer_add_str (buf, " \"-\" \""); } if (useragent->len > 0) { cherokee_buffer_add_buffer (buf, useragent); } cherokee_buffer_add_str (buf, "\"\n"); return ret_ok; }