HTTP redirection added in the API.

This commit is contained in:
Jose Marcial Vieira Bisneto 2017-09-21 11:50:15 -03:00
parent b301c7e4d2
commit e0d5fed53e
No known key found for this signature in database
GPG key ID: 103E935E7E6E831E
2 changed files with 34 additions and 9 deletions

View file

@ -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.

View file

@ -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" \
"<a href=\"%s\">Moved Permanently</a>.\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" \
"<a href=\"%s\">Found</a>.\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" \