From e0d5fed53eaa1d6df5c037d9a5fca076c81887e5 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 21 Sep 2017 11:50:15 -0300 Subject: [PATCH] HTTP redirection added in the API. --- core/api.c | 29 ++++++++++++++++++++--------- include/ipfs/core/api.h | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/core/api.c b/core/api.c index fa99402..ae6075f 100644 --- a/core/api.c +++ b/core/api.c @@ -427,19 +427,30 @@ void *api_connection_thread (void *ptr) if (strcmp (req.buf + req.path, "/")==0 || strcmp (req.buf + req.path, "/webui") || strcmp (req.buf + req.path, "/webui/")==0) { - path = "/ipfs/QmPhnvn747LqwPYMJmQVorMaGbMSgA7mRRoyyZYz3DoZRQ/"; // WEBUI PAGE + char *redir; + size_t size = sizeof(HTTP_301) + (sizeof(WEBUI_ADDR)*2); + + redir = malloc(size); + if (redir) { + snprintf(redir, size, HTTP_301, WEBUI_ADDR, WEBUI_ADDR); + redir[size-1] = '\0'; // just in case + write_dual (s, req.buf + req.http_ver, strchr (redir, ' ')); + free (redir); + } else { + write_cstr (s, HTTP_500); + } } else { path = req.buf + req.path; - } - if (get_object(params->this_node, path, &obj, &size)) { - if (!send_object(s, obj, size)) { - libp2p_logger_error("api", "fail send_object.\n"); + if (get_object(params->this_node, path, &obj, &size)) { + if (!send_object(s, obj, size)) { + libp2p_logger_error("api", "fail send_object.\n"); + } + free(obj); + } else { + // object not found. + write_dual (s, req.buf + req.http_ver, strchr (HTTP_404, ' ')); } - free(obj); - } else { - // object not found. - write_dual (s, req.buf + req.http_ver, strchr (HTTP_404, ' ')); } } else if (strcmp(buf + req.method, "POST")==0) { // TODO: Handle gzip/json POST requests. diff --git a/include/ipfs/core/api.h b/include/ipfs/core/api.h index e6bc12a..85fbfa8 100644 --- a/include/ipfs/core/api.h +++ b/include/ipfs/core/api.h @@ -39,6 +39,20 @@ struct s_request { size_t boundary_size; }; +#define WEBUI_ADDR "/ipfs/QmPhnvn747LqwPYMJmQVorMaGbMSgA7mRRoyyZYz3DoZRQ/" + +#define HTTP_301 "HTTP/1.1 301 Moved Permanently\r\n" \ + "Location: %s\r\n" \ + "Content-Type: text/html\r\n\r\n" \ + "Moved Permanently.\r\n\r\n" + +#define HTTP_302 "HTTP/1.1 302 Found\r\n" \ + "Content-Type: text/html\r\n" \ + "Connection: close\r\n" \ + "Location: %s\r\n" \ + "X-Ipfs-Path: %s\r\n\r\n" \ + "Found.\r\n\r\n" + #define HTTP_400 "HTTP/1.1 400 Bad Request\r\n" \ "Content-Type: text/plain\r\n" \ "Connection: close\r\n\r\n" \