[sheepdog] [PATCH 5/9] http: move some structures and definitions to http.h
MORITA Kazutaka
morita.kazutaka at gmail.com
Thu Oct 31 08:49:03 CET 2013
From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
This prepares for introducing http driver implementation.
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
sheep/Makefile.am | 2 +-
sheep/http.c | 76 +++++++++++++++++++++++++++++--------------------------
sheep/http.h | 60 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 101 insertions(+), 37 deletions(-)
create mode 100644 sheep/http.h
diff --git a/sheep/Makefile.am b/sheep/Makefile.am
index 97686b5..09311ff 100644
--- a/sheep/Makefile.am
+++ b/sheep/Makefile.am
@@ -51,7 +51,7 @@ sheep_LDADD = ../lib/libsheepdog.a -lpthread -lm\
sheep_DEPENDENCIES = ../lib/libsheepdog.a
-noinst_HEADERS = sheep_priv.h cluster.h trace/trace.h
+noinst_HEADERS = sheep_priv.h cluster.h http.h trace/trace.h
EXTRA_DIST =
diff --git a/sheep/http.c b/sheep/http.c
index 9e5551e..2585daa 100644
--- a/sheep/http.c
+++ b/sheep/http.c
@@ -13,51 +13,46 @@
/* This files implement RESTful interface to sheepdog storage via fastcgi */
-#include <fcgiapp.h>
-
+#include "http.h"
#include "sheep_priv.h"
-enum http_opcode {
- HTTP_GET = 1,
- HTTP_PUT,
- HTTP_POST,
- HTTP_DELETE,
- HTTP_HEAD,
-};
+static inline const char *stropcode(enum http_opcode opcode)
+{
+ static const char *const descs[] = {
+ [HTTP_GET] = "GET",
+ [HTTP_PUT] = "PUT",
+ [HTTP_POST] = "POST",
+ [HTTP_DELETE] = "DELETE",
+ [HTTP_HEAD] = "HEAD",
+ };
-enum http_status {
- UNKNOWN = 0,
- OK, /* 200 */
- CREATED, /* 201 */
- PARTIAL_CONTENT, /* 206 */
- BAD_REQUEST, /* 400 */
- NOT_FOUND, /* 404 */
- REQUEST_RANGE_NOT_SATISFIABLE, /* 416 */
- INTERNAL_SERVER_ERROR, /* 500 */
- NOT_IMPLEMENTED, /* 501 */
-};
+ if (descs[opcode] == NULL) {
+ static __thread char msg[32];
+ snprintf(msg, sizeof(msg), "Invalid opcode %d", opcode);
+ return msg;
+ }
-struct http_request {
- FCGX_Request fcgx;
- char *uri;
- enum http_opcode opcode;
- enum http_status status;
- size_t data_length;
-};
+ return descs[opcode];
+}
-static inline const char *strstatus(int status)
+static inline const char *strstatus(enum http_status status)
{
static const char *const descs[] = {
[UNKNOWN] = "Unknown",
[OK] = "200 OK",
- [CREATED] = "201 CREATED",
+ [CREATED] = "201 Created",
+ [ACCEPTED] = "202 Accepted",
+ [NO_CONTENT] = "204 No Content",
[PARTIAL_CONTENT] = "206 Partial Content",
[BAD_REQUEST] = "400 Bad Request",
[NOT_FOUND] = "404 Not Found",
+ [METHOD_NOT_ALLOWED] = "405 Method Not Allowed",
+ [CONFLICT] = "409 Conflict",
[REQUEST_RANGE_NOT_SATISFIABLE] =
"416 Requested Range Not Satisfiable",
[INTERNAL_SERVER_ERROR] = "500 Internal Server Error",
[NOT_IMPLEMENTED] = "501 Not Implemented",
+ [SERVICE_UNAVAILABLE] = "503 Service_Unavailable",
};
if (descs[status] == NULL) {
@@ -69,6 +64,17 @@ static inline const char *strstatus(int status)
return descs[status];
}
+const char *str_http_req(const struct http_request *req)
+{
+ static __thread char msg[1024];
+
+ snprintf(msg, sizeof(msg), "%s %s, status = %s, data_length = %zd",
+ req->uri, stropcode(req->opcode), strstatus(req->status),
+ req->data_length);
+
+ return msg;
+}
+
struct http_work {
struct work work;
struct http_request *request;
@@ -86,8 +92,7 @@ static inline void http_request_error(struct http_request *req)
sd_err("failed, %s", strerror(ret));
}
-static inline int http_request_write(struct http_request *req,
- const char *buf, int len)
+int http_request_write(struct http_request *req, const void *buf, int len)
{
int ret = FCGX_PutStr(buf, len, req->fcgx.out);
if (ret < 0)
@@ -95,8 +100,7 @@ static inline int http_request_write(struct http_request *req,
return ret;
}
-static inline int http_request_read(struct http_request *req,
- char *buf, int len)
+int http_request_read(struct http_request *req, void *buf, int len)
{
int ret = FCGX_GetStr(buf, len, req->fcgx.in);
if (ret < 0)
@@ -104,7 +108,7 @@ static inline int http_request_read(struct http_request *req,
return ret;
}
-static inline int http_request_writes(struct http_request *req, const char *str)
+int http_request_writes(struct http_request *req, const char *str)
{
int ret = FCGX_PutS(str, req->fcgx.out);
if (ret < 0)
@@ -113,7 +117,7 @@ static inline int http_request_writes(struct http_request *req, const char *str)
}
__printf(2, 3)
-static int http_request_writef(struct http_request *req, const char *fmt, ...)
+int http_request_writef(struct http_request *req, const char *fmt, ...)
{
va_list ap;
int ret;
@@ -168,7 +172,7 @@ static int http_init_request(struct http_request *req)
}
/* This function does nothing if we have already printed a status code. */
-static void http_response_header(struct http_request *req, int status)
+void http_response_header(struct http_request *req, enum http_status status)
{
if (req->status != UNKNOWN)
return;
diff --git a/sheep/http.h b/sheep/http.h
new file mode 100644
index 0000000..0f30ea4
--- /dev/null
+++ b/sheep/http.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2013 MORITA Kazutaka <morita.kazutaka at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __SHEEP_HTTP_H__
+#define __SHEEP_HTTP_H__
+
+#include <fcgiapp.h>
+
+#include "sheepdog_proto.h"
+#include "sheep.h"
+
+enum http_opcode {
+ HTTP_GET = 1,
+ HTTP_PUT,
+ HTTP_POST,
+ HTTP_DELETE,
+ HTTP_HEAD,
+};
+
+enum http_status {
+ UNKNOWN = 0,
+ OK, /* 200 */
+ CREATED, /* 201 */
+ ACCEPTED, /* 202 */
+ NO_CONTENT, /* 204 */
+ PARTIAL_CONTENT, /* 206 */
+ BAD_REQUEST, /* 400 */
+ NOT_FOUND, /* 404 */
+ METHOD_NOT_ALLOWED, /* 405 */
+ CONFLICT, /* 409 */
+ REQUEST_RANGE_NOT_SATISFIABLE, /* 416 */
+ INTERNAL_SERVER_ERROR, /* 500 */
+ NOT_IMPLEMENTED, /* 501 */
+ SERVICE_UNAVAILABLE, /* 503 */
+};
+
+struct http_request {
+ FCGX_Request fcgx;
+ char *uri;
+ enum http_opcode opcode;
+ enum http_status status;
+ size_t data_length;
+};
+
+const char *str_http_req(const struct http_request *req);
+void http_response_header(struct http_request *req, enum http_status status);
+int http_request_read(struct http_request *req, void *buf, int len);
+int http_request_write(struct http_request *req, const void *buf, int len);
+int http_request_writes(struct http_request *req, const char *str);
+__printf(2, 3)
+int http_request_writef(struct http_request *req, const char *fmt, ...);
+
+#endif /* __SHEEP_HTTP_H__ */
--
1.8.1.2
More information about the sheepdog
mailing list