Move HTTP error codes into http.h.
authorreyk <reyk@openbsd.org>
Sat, 12 Jul 2014 14:34:13 +0000 (14:34 +0000)
committerreyk <reyk@openbsd.org>
Sat, 12 Jul 2014 14:34:13 +0000 (14:34 +0000)
ok benno@

usr.sbin/relayd/http.h
usr.sbin/relayd/log.c
usr.sbin/relayd/relay_http.c
usr.sbin/relayd/relayd.h

index 0cf9b79..69343cb 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: http.h,v 1.2 2014/07/11 11:48:50 reyk Exp $   */
+/*     $OpenBSD: http.h,v 1.3 2014/07/12 14:34:13 reyk Exp $   */
 
 /*
  * Copyright (c) 2012 - 2014 Reyk Floeter <reyk@openbsd.org>
@@ -72,6 +72,53 @@ struct http_method {
        { HTTP_METHOD_NONE,             NULL }          \
 }
 
+struct http_error {
+       int                      error_code;
+       const char              *error_name;
+};
+#define HTTP_ERRORS            {                       \
+       { 100,  "Continue" },                           \
+       { 101,  "Switching Protocols" },                \
+       { 200,  "OK" },                                 \
+       { 201,  "Created" },                            \
+       { 202,  "Accepted" },                           \
+       { 203,  "Non-Authorative Information" },        \
+       { 204,  "No Content" },                         \
+       { 205,  "Reset Content" },                      \
+       { 206,  "Partial Content" },                    \
+       { 300,  "Multiple Choices" },                   \
+       { 301,  "Moved Permanently" },                  \
+       { 302,  "Moved Temporarily" },                  \
+       { 303,  "See Other" },                          \
+       { 304,  "Not Modified" },                       \
+       { 307,  "Temporary Redirect" },                 \
+       { 400,  "Bad Request" },                        \
+       { 401,  "Unauthorized" },                       \
+       { 402,  "Payment Required" },                   \
+       { 403,  "Forbidden" },                          \
+       { 404,  "Not Found" },                          \
+       { 405,  "Method Not Allowed" },                 \
+       { 406,  "Not Acceptable" },                     \
+       { 407,  "Proxy Authentication Required" },      \
+       { 408,  "Request Timeout" },                    \
+       { 409,  "Conflict" },                           \
+       { 410,  "Gone" },                               \
+       { 411,  "Length Required" },                    \
+       { 412,  "Precondition Failed" },                \
+       { 413,  "Request Entity Too Large" },           \
+       { 414,  "Request-URL Too Long" },               \
+       { 415,  "Unsupported Media Type" },             \
+       { 416,  "Requested Range Not Satisfiable" },    \
+       { 417,  "Expectation Failed" },                 \
+       { 500,  "Internal Server Error" },              \
+       { 501,  "Not Implemented" },                    \
+       { 502,  "Bad Gateway" },                        \
+       { 503,  "Service Unavailable" },                \
+       { 504,  "Gateway Timeout" },                    \
+       { 505,  "HTTP Version Not Supported" },         \
+       { 0,    NULL }                                  \
+}
+
 /* Used during runtime */
 struct http_descriptor {
        struct kv                http_pathquery;
index 204e273..f2a6d32 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: log.c,v 1.22 2014/04/18 16:13:02 reyk Exp $   */
+/*     $OpenBSD: log.c,v 1.23 2014/07/12 14:34:13 reyk Exp $   */
 
 /*
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -365,62 +365,6 @@ print_time(struct timeval *a, struct timeval *b, char *buf, size_t len)
        return (buf);
 }
 
-const char *
-print_httperror(u_int code)
-{
-       u_int                    i;
-       struct {
-               u_int            ht_code;
-               const char      *ht_err;
-       }                        httperr[] = {
-               { 100, "Continue" },
-               { 101, "Switching Protocols" },
-               { 200, "OK" },
-               { 201, "Created" },
-               { 202, "Accepted" },
-               { 203, "Non-Authorative Information" },
-               { 204, "No Content" },
-               { 205, "Reset Content" },
-               { 206, "Partial Content" },
-               { 300, "Multiple Choices" },
-               { 301, "Moved Permanently" },
-               { 302, "Moved Temporarily" },
-               { 303, "See Other" },
-               { 304, "Not Modified" },
-               { 307, "Temporary Redirect" },
-               { 400, "Bad Request" },
-               { 401, "Unauthorized" },
-               { 402, "Payment Required" },
-               { 403, "Forbidden" },
-               { 404, "Not Found" },
-               { 405, "Method Not Allowed" },
-               { 406, "Not Acceptable" },
-               { 407, "Proxy Authentication Required" },
-               { 408, "Request Timeout" },
-               { 409, "Conflict" },
-               { 410, "Gone" },
-               { 411, "Length Required" },
-               { 412, "Precondition Failed" },
-               { 413, "Request Entity Too Large" },
-               { 414, "Request-URL Too Long" },
-               { 415, "Unsupported Media Type" },
-               { 416, "Requested Range Not Satisfiable" },
-               { 417, "Expectation Failed" },
-               { 500, "Internal Server Error" },
-               { 501, "Not Implemented" },
-               { 502, "Bad Gateway" },
-               { 503, "Service Unavailable" },
-               { 504, "Gateway Timeout" },
-               { 505, "HTTP Version Not Supported" },
-               { 0 }
-       };
-
-       for (i = 0; httperr[i].ht_code != 0; i++)
-               if (httperr[i].ht_code == code)
-                       return (httperr[i].ht_err);
-       return ("Unknown Error");
-}
-
 const char *
 printb_flags(const u_int32_t v, const char *bits)
 {
index edfdd3e..4928fcf 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: relay_http.c,v 1.25 2014/07/11 23:11:54 benno Exp $   */
+/*     $OpenBSD: relay_http.c,v 1.26 2014/07/12 14:34:13 reyk Exp $    */
 
 /*
  * Copyright (c) 2006 - 2014 Reyk Floeter <reyk@openbsd.org>
@@ -68,6 +68,7 @@ int            relay_writeresponse_http(struct ctl_relay_event *,
                    struct ctl_relay_event *);
 void            relay_reset_http(struct ctl_relay_event *);
 static int      relay_httpmethod_cmp(const void *, const void *);
+static int      relay_httperror_cmp(const void *, const void *);
 int             relay_httpquery_test(struct ctl_relay_event *,
                    struct relay_rule *, struct kvlist *);
 int             relay_httpheader_test(struct ctl_relay_event *,
@@ -86,6 +87,7 @@ void           relay_httpdesc_free(struct http_descriptor *);
 static struct relayd   *env = NULL;
 
 static struct http_method       http_methods[] = HTTP_METHODS;
+static struct http_error        http_errors[] = HTTP_ERRORS;
 
 void
 relay_http(struct relayd *x_env)
@@ -99,6 +101,9 @@ relay_http(struct relayd *x_env)
        qsort(http_methods, sizeof(http_methods) /
            sizeof(http_methods[0]) - 1,
            sizeof(http_methods[0]), relay_httpmethod_cmp);
+       qsort(http_errors, sizeof(http_errors) /
+           sizeof(http_errors[0]) - 1,
+           sizeof(http_errors[0]), relay_httperror_cmp);
 }
 
 void
@@ -870,13 +875,16 @@ relay_abort_http(struct rsession *con, u_int code, const char *msg,
 {
        struct relay            *rlay = con->se_relay;
        struct bufferevent      *bev = con->se_in.bev;
-       const char              *httperr = print_httperror(code), *text = "";
+       const char              *httperr = NULL, *text = "";
        char                    *httpmsg;
        time_t                   t;
        struct tm               *lt;
        char                     tmbuf[32], hbuf[128];
        const char              *style, *label = NULL;
 
+       if ((httperr = relay_httperror_byid(code)) == NULL)
+               httperr = "Unknown Error";
+
        if (labelid != 0)
                label = label_id2name(labelid);
 
@@ -1159,6 +1167,29 @@ relay_httpmethod_cmp(const void *a, const void *b)
        return (strcmp(ma->method_name, mb->method_name));
 }
 
+const char *
+relay_httperror_byid(u_int id)
+{
+       struct http_error        error, *res = NULL;
+
+       /* Set up key */
+       error.error_code = (int)id;
+
+       res = bsearch(&error, http_errors,
+           sizeof(http_errors) / sizeof(http_errors[0]) - 1,
+           sizeof(http_errors[0]), relay_httperror_cmp);
+
+       return (res->error_name);
+}
+
+static int
+relay_httperror_cmp(const void *a, const void *b)
+{
+       const struct http_error *ea = a;
+       const struct http_error *eb = b;
+       return (ea->error_code - eb->error_code);
+}
+
 int
 relay_httpquery_test(struct ctl_relay_event *cre, struct relay_rule *rule,
     struct kvlist *actions)
index 88c27b4..b103eda 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: relayd.h,v 1.186 2014/07/11 22:28:44 reyk Exp $       */
+/*     $OpenBSD: relayd.h,v 1.187 2014/07/12 14:34:13 reyk Exp $       */
 
 /*
  * Copyright (c) 2006 - 2014 Reyk Floeter <reyk@openbsd.org>
@@ -1075,7 +1075,6 @@ const char *table_check(enum table_check);
 const char *print_availability(u_long, u_long);
 const char *print_host(struct sockaddr_storage *, char *, size_t);
 const char *print_time(struct timeval *, struct timeval *, char *, size_t);
-const char *print_httperror(u_int);
 const char *printb_flags(const u_int32_t, const char *);
 void    getmonotime(struct timeval *);
 
@@ -1162,6 +1161,8 @@ void       relay_close_http(struct rsession *);
 u_int   relay_httpmethod_byname(const char *);
 const char
        *relay_httpmethod_byid(u_int);
+const char
+       *relay_httperror_byid(u_int);
 int     relay_httpdesc_init(struct ctl_relay_event *);
 
 /* relay_udp.c */