[Olsr-dev] [PATCH v1 05/10] info: prepare for more HTTP status codes
Ferry Huberts
(spam-protected)
Sun Feb 21 18:56:18 CET 2016
From: Ferry Huberts <(spam-protected)>
Signed-off-by: Ferry Huberts <(spam-protected)>
---
lib/info/http_headers.c | 5 ++---
lib/info/http_headers.h | 14 ++++++++++++--
lib/info/olsrd_info.c | 10 ++++++----
3 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/lib/info/http_headers.c b/lib/info/http_headers.c
index b6d7438..e15d4e5 100644
--- a/lib/info/http_headers.c
+++ b/lib/info/http_headers.c
@@ -43,14 +43,13 @@
#include <assert.h>
-void http_header_build(const char *plugin_name, const char *status, const char *mime, struct autobuf *abuf, int *contentLengthIndex) {
+void http_header_build(const char *plugin_name, unsigned int status, const char *mime, struct autobuf *abuf, int *contentLengthIndex) {
assert(plugin_name);
- assert(status);
assert(abuf);
assert(contentLengthIndex);
/* Status */
- abuf_appendf(abuf, "%s\r\n", status);
+ abuf_appendf(abuf, "%s\r\n", httpStatusToReply(status));
/* Date */
{
diff --git a/lib/info/http_headers.h b/lib/info/http_headers.h
index 97908a3..308c561 100644
--- a/lib/info/http_headers.h
+++ b/lib/info/http_headers.h
@@ -44,11 +44,21 @@
#include "common/autobuf.h"
+#define INFO_HTTP_VERSION "HTTP/1.1"
+
/* Response types */
-#define INFO_HTTP_OK "HTTP/1.1 200 OK"
+#define INFO_HTTP_OK (200)
-void http_header_build(const char * plugin_name, const char *status, const char *mime, struct autobuf *abuf, int *contentLengthIndex);
+void http_header_build(const char * plugin_name, unsigned int status, const char *mime, struct autobuf *abuf, int *contentLengthIndex);
void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthIndex, int contentLength);
+static INLINE const char * httpStatusToReply(unsigned int status) {
+ switch (status) {
+ case INFO_HTTP_OK:
+ default:
+ return INFO_HTTP_VERSION " 200 OK";
+ }
+}
+
#endif /* _OLSRD_LIB_INFO_HTTP_HEADERS_H_ */
diff --git a/lib/info/olsrd_info.c b/lib/info/olsrd_info.c
index 866db8c..63c343e 100644
--- a/lib/info/olsrd_info.c
+++ b/lib/info/olsrd_info.c
@@ -187,7 +187,7 @@ static void write_data(void *foo __attribute__ ((unused))) {
}
}
-static void send_info(unsigned int send_what, int the_socket) {
+static void send_info(const char * req __attribute__((unused)), unsigned int send_what, int the_socket, unsigned int status) {
struct autobuf abuf;
const char *content_type = functions->determine_mime_type ? functions->determine_mime_type(send_what) : "text/plain; charset=utf-8";
@@ -197,7 +197,7 @@ static void send_info(unsigned int send_what, int the_socket) {
abuf_init(&abuf, 2 * 4096);
if (config->http_headers) {
- http_header_build(name, INFO_HTTP_OK, content_type, &abuf, &contentLengthIndex);
+ http_header_build(name, status, content_type, &abuf, &contentLengthIndex);
headerLength = abuf.len;
}
@@ -357,6 +357,8 @@ static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int
char addr[INET6_ADDRSTRLEN];
#endif /* NODEBUG */
+ char * req = NULL;
+ unsigned int http_status = INFO_HTTP_OK;
union olsr_sockaddr sock_addr;
socklen_t sock_addr_len = sizeof(sock_addr);
fd_set rfds;
@@ -425,7 +427,7 @@ static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int
}
if (0 <= s) {
- char * req = requ;
+ req = requ;
req[s] = '\0';
req = parseRequest(req, (size_t*)&s);
if ((req[0] == '\0') || ((req[0] == '/') && (req[1] == '\0'))) {
@@ -441,7 +443,7 @@ static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int
}
}
- send_info(send_what, ipc_connection);
+ send_info(req ? req : "", send_what, ipc_connection, http_status);
}
static int plugin_ipc_init(void) {
--
2.5.0
More information about the Olsr-dev
mailing list