Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 353 lines (319 sloc) 12.704 kb
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include "common-internal.h"
#include "http.h"

#include <stdio.h>

#define entry(name,name_str) \
case name: \
if (len) *len = sizeof(name_str) - 1; \
*str = name_str; \
return ret_ok;


ret_t
cherokee_http_method_to_string (cherokee_http_method_t method, const char **str, cuint_t *len)
{
switch (method) {
/* HTTP 1.1 methods
*/
entry (http_get, "GET");
entry (http_post, "POST");
entry (http_head, "HEAD");
entry (http_put, "PUT");
entry (http_options, "OPTIONS");
entry (http_delete, "DELETE");
entry (http_trace, "TRACE");
entry (http_connect, "CONNECT");

/* WebDAV methods
*/
entry (http_copy, "COPY");
entry (http_lock, "LOCK");
entry (http_mkcol, "MKCOL");
entry (http_move, "MOVE");
entry (http_notify, "NOTIFY");
entry (http_poll, "POLL");
entry (http_propfind, "PROPFIND");
entry (http_proppatch, "PROPPATCH");
entry (http_search, "SEARCH");
entry (http_subscribe, "SUBSCRIBE");
entry (http_unlock, "UNLOCK");
entry (http_unsubscribe, "UNSUBSCRIBE");
entry (http_report, "REPORT");
entry (http_patch, "PATCH");
entry (http_version_control, "VERSION_CONTROL");
entry (http_checkout, "CHECKOUT");
entry (http_uncheckout, "UNCHECKOUT");
entry (http_checkin, "CHECKIN");
entry (http_update, "UPDATE");
entry (http_label, "LABEL");
entry (http_mkworkspace, "MKWORKSPACE");
entry (http_mkactivity, "MKACTIVITY");
entry (http_baseline_control, "BASELINE_CONTROL");
entry (http_merge, "MERGE");
entry (http_invalid, "INVALID");

default:
break;
};

if (len) *len = 7;
*str = "UNKNOWN";
return ret_error;
}


ret_t
cherokee_http_string_to_method (cherokee_buffer_t *string,
cherokee_http_method_t *method)
{
if (cherokee_buffer_case_cmp_str (string, "get") == 0)
*method = http_get;
else if (cherokee_buffer_case_cmp_str (string, "post") == 0)
*method = http_post;
else if (cherokee_buffer_case_cmp_str (string, "head") == 0)
*method = http_head;
else if (cherokee_buffer_case_cmp_str (string, "put") == 0)
*method = http_put;
else if (cherokee_buffer_case_cmp_str (string, "options") == 0)
*method = http_options;
else if (cherokee_buffer_case_cmp_str (string, "delete") == 0)
*method = http_delete;
else if (cherokee_buffer_case_cmp_str (string, "trace") == 0)
*method = http_trace;
else if (cherokee_buffer_case_cmp_str (string, "connect") == 0)
*method = http_connect;
else if (cherokee_buffer_case_cmp_str (string, "copy") == 0)
*method = http_copy;
else if (cherokee_buffer_case_cmp_str (string, "lock") == 0)
*method = http_lock;
else if (cherokee_buffer_case_cmp_str (string, "mkcol") == 0)
*method = http_mkcol;
else if (cherokee_buffer_case_cmp_str (string, "move") == 0)
*method = http_move;
else if (cherokee_buffer_case_cmp_str (string, "notify") == 0)
*method = http_notify;
else if (cherokee_buffer_case_cmp_str (string, "poll") == 0)
*method = http_poll;
else if (cherokee_buffer_case_cmp_str (string, "propfind") == 0)
*method = http_propfind;
else if (cherokee_buffer_case_cmp_str (string, "proppatch") == 0)
*method = http_proppatch;
else if (cherokee_buffer_case_cmp_str (string, "search") == 0)
*method = http_search;
else if (cherokee_buffer_case_cmp_str (string, "subscribe") == 0)
*method = http_subscribe;
else if (cherokee_buffer_case_cmp_str (string, "unlock") == 0)
*method = http_unlock;
else if (cherokee_buffer_case_cmp_str (string, "unsubscribe") == 0)
*method = http_unsubscribe;
else if (cherokee_buffer_case_cmp_str (string, "report") == 0)
*method = http_report;
else if (cherokee_buffer_case_cmp_str (string, "patch") == 0)
*method = http_patch;
else if (cherokee_buffer_case_cmp_str (string, "version_control") == 0)
*method = http_version_control;
else if (cherokee_buffer_case_cmp_str (string, "checkout") == 0)
*method = http_checkout;
else if (cherokee_buffer_case_cmp_str (string, "uncheckout") == 0)
*method = http_uncheckout;
else if (cherokee_buffer_case_cmp_str (string, "checkin") == 0)
*method = http_checkin;
else if (cherokee_buffer_case_cmp_str (string, "update") == 0)
*method = http_update;
else if (cherokee_buffer_case_cmp_str (string, "label") == 0)
*method = http_label;
else if (cherokee_buffer_case_cmp_str (string, "mkworkspace") == 0)
*method = http_mkworkspace;
else if (cherokee_buffer_case_cmp_str (string, "mkactivity") == 0)
*method = http_mkactivity;
else if (cherokee_buffer_case_cmp_str (string, "baseline_control") == 0)
*method = http_baseline_control;
else if (cherokee_buffer_case_cmp_str (string, "merge") == 0)
*method = http_merge;
else if (cherokee_buffer_case_cmp_str (string, "invalid") == 0)
*method = http_invalid;
else if (cherokee_buffer_case_cmp_str (string, "purge") == 0)
*method = http_purge;
else {
*method = http_unknown;
return ret_not_found;
}

return ret_ok;
}


ret_t
cherokee_http_version_to_string (cherokee_http_version_t version, const char **str, cuint_t *len)
{
switch (version) {
entry (http_version_11, "HTTP/1.1");
entry (http_version_10, "HTTP/1.0");
entry (http_version_09, "HTTP/0.9");
default:
break;
};

if (len) *len = 12;
*str = "HTTP/Unknown";
return ret_error;
}


ret_t
cherokee_http_code_to_string (cherokee_http_t code, const char **str)
{
switch (code) {

/* 2xx
*/
case http_ok: *str = http_ok_string; break;
case http_created: *str = http_created_string; break;
case http_accepted: *str = http_accepted_string; break;
case http_non_authoritative_info: *str = http_non_authoritative_info_string; break;
case http_no_content: *str = http_no_content_string; break;
case http_reset_content: *str = http_reset_content_string; break;
case http_partial_content: *str = http_partial_content_string; break;
case http_multi_status: *str = http_multi_status_string; break;
case http_im_used: *str = http_im_used_string; break;

/* 3xx
*/
case http_moved_permanently: *str = http_moved_permanently_string; break;
case http_moved_temporarily: *str = http_moved_temporarily_string; break;
case http_see_other: *str = http_see_other_string; break;
case http_not_modified: *str = http_not_modified_string; break;
case http_temporary_redirect: *str = http_temporary_redirect_string; break;
case http_multiple_choices: *str = http_multiple_choices_string; break;
case http_use_proxy: *str = http_use_proxy_string; break;

/* 4xx
*/
case http_bad_request: *str = http_bad_request_string; break;
case http_unauthorized: *str = http_unauthorized_string; break;
case http_access_denied: *str = http_access_denied_string; break;
case http_not_found: *str = http_not_found_string; break;
case http_method_not_allowed: *str = http_method_not_allowed_string; break;
case http_not_acceptable: *str = http_not_acceptable_string; break;
case http_request_timeout: *str = http_request_timeout_string; break;
case http_gone: *str = http_gone_string; break;
case http_length_required: *str = http_length_required_string; break;
case http_request_entity_too_large: *str = http_request_entity_too_large_string; break;
case http_unsupported_media_type: *str = http_unsupported_media_type_string; break;
case http_request_uri_too_long: *str = http_request_uri_too_long_string; break;
case http_range_not_satisfiable: *str = http_range_not_satisfiable_string; break;
case http_upgrade_required: *str = http_upgrade_required_string; break;
case http_payment_required: *str = http_payment_required_string; break;
case http_proxy_auth_required: *str = http_proxy_auth_required_string; break;
case http_conflict: *str = http_conflict_string; break;
case http_precondition_failed: *str = http_precondition_failed_string; break;
case http_expectation_failed: *str = http_expectation_failed_string; break;
case http_unprocessable_entity: *str = http_unprocessable_entity_string; break;
case http_locked: *str = http_locked_string; break;
case http_failed_dependency: *str = http_failed_dependency_string; break;
case http_unordered_collection: *str = http_unordered_collection_string; break;
case http_retry_with: *str = http_retry_with_string; break;

/* 5xx
*/
case http_internal_error: *str = http_internal_error_string; break;
case http_not_implemented: *str = http_not_implemented_string; break;
case http_bad_gateway: *str = http_bad_gateway_string; break;
case http_service_unavailable: *str = http_service_unavailable_string; break;
case http_gateway_timeout: *str = http_gateway_timeout_string; break;
case http_version_not_supported: *str = http_version_not_supported_string; break;
case http_variant_also_negotiates: *str = http_variant_also_negotiates_string; break;
case http_insufficient_storage: *str = http_insufficient_storage_string; break;
case http_bandwidth_limit_exceeded: *str = http_bandwidth_limit_exceeded_string; break;
case http_not_extended: *str = http_not_extended_string; break;

/* 1xx
*/
case http_continue: *str = http_continue_string; break;
case http_processing: *str = http_processing_string; break;

default:
*str = "500 Unknown error";
return ret_error;
}

return ret_ok;
}


ret_t
cherokee_http_code_copy (cherokee_http_t code, cherokee_buffer_t *buf)
{
#define entry_code(c) \
case http_ ## c: \
return cherokee_buffer_add (buf, http_ ## c ## _string, sizeof(http_ ## c ## _string)-1)

switch (code) {

/* 2xx
*/
entry_code (ok);
entry_code (created);
entry_code (accepted);
entry_code (non_authoritative_info);
entry_code (no_content);
entry_code (reset_content);
entry_code (partial_content);
entry_code (multi_status);
entry_code (im_used);

/* 3xx
*/
entry_code (moved_permanently);
entry_code (moved_temporarily);
entry_code (see_other);
entry_code (not_modified);
entry_code (temporary_redirect);
entry_code (multiple_choices);
entry_code (use_proxy);

/* 4xx
*/
entry_code (bad_request);
entry_code (unauthorized);
entry_code (access_denied);
entry_code (not_found);
entry_code (method_not_allowed);
entry_code (not_acceptable);
entry_code (request_timeout);
entry_code (length_required);
entry_code (gone);
entry_code (request_entity_too_large);
entry_code (request_uri_too_long);
entry_code (unsupported_media_type);
entry_code (range_not_satisfiable);
entry_code (upgrade_required);
entry_code (payment_required);
entry_code (proxy_auth_required);
entry_code (conflict);
entry_code (precondition_failed);
entry_code (expectation_failed);
entry_code (unprocessable_entity);
entry_code (locked);
entry_code (failed_dependency);
entry_code (unordered_collection);
entry_code (retry_with);

/* 5xx
*/
entry_code (internal_error);
entry_code (not_implemented);
entry_code (bad_gateway);
entry_code (service_unavailable);
entry_code (gateway_timeout);
entry_code (version_not_supported);
entry_code (insufficient_storage);
entry_code (bandwidth_limit_exceeded);
entry_code (not_extended);
entry_code (variant_also_negotiates);

/* 1xx
*/
entry_code (continue);
entry_code (processing);

default:
  LOG_WARNING (CHEROKEE_ERROR_HTTP_UNKNOWN_CODE, code);
  cherokee_buffer_add_str (buf, http_internal_error_string);
  return ret_error;
}

SHOULDNT_HAPPEN;
return ret_error;
}
Something went wrong with that request. Please try again.