redirect: New member function for `struct uh_client`

cl->redirect(cl, 302, "/xx.html");
cl->redirect(cl, 301, "/%s.html", "xx");

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2017-12-30 15:57:48 +08:00
parent 59a60eb2ea
commit b1cf13013e
4 changed files with 21 additions and 4 deletions

View File

@ -62,6 +62,23 @@ static inline void client_header_end(struct uh_client *cl)
cl->printf(cl, "\r\n"); cl->printf(cl, "\r\n");
} }
static inline void client_redirect(struct uh_client *cl, int code, const char *fmt, ...)
{
va_list arg;
const char *summary = ((code == 301) ? "Moved Permanently" : "Found");
assert((code == 301 || code == 302) && fmt);
cl->send_header(cl, code, summary, 0);
cl->printf(cl, "Location: ");
va_start(arg, fmt);
cl->vprintf(cl, fmt, arg);
va_end(arg);
cl->printf(cl, "\r\n\r\n");
cl->request_done(cl);
}
static void client_send_error(struct uh_client *cl, int code, const char *summary, const char *fmt, ...) static void client_send_error(struct uh_client *cl, int code, const char *summary, const char *fmt, ...)
{ {
va_list arg; va_list arg;
@ -479,6 +496,7 @@ void uh_accept_client(struct uh_server *srv, bool ssl)
cl->send_header = client_send_header; cl->send_header = client_send_header;
cl->append_header = client_append_header; cl->append_header = client_append_header;
cl->header_end = client_header_end; cl->header_end = client_header_end;
cl->redirect = client_redirect;
cl->request_done = client_request_done; cl->request_done = client_request_done;
cl->send = client_send; cl->send = client_send;

View File

@ -94,6 +94,7 @@ struct uh_client {
void (*send_header)(struct uh_client *cl, int code, const char *summary, int length); void (*send_header)(struct uh_client *cl, int code, const char *summary, int length);
void (*append_header)(struct uh_client *cl, const char *name, const char *value); void (*append_header)(struct uh_client *cl, const char *name, const char *value);
void (*header_end)(struct uh_client *cl); void (*header_end)(struct uh_client *cl);
void (*redirect)(struct uh_client *cl, int code, const char *fmt, ...);
void (*request_done)(struct uh_client *cl); void (*request_done)(struct uh_client *cl);
void (*send)(struct uh_client *cl, const void *data, int len); void (*send)(struct uh_client *cl, const void *data, int len);

View File

@ -25,6 +25,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <inttypes.h> #include <inttypes.h>

View File

@ -227,10 +227,7 @@ struct path_info *uh_path_lookup(struct uh_client *cl, const char *url)
is missing in the request url, redirect the client to the same is missing in the request url, redirect the client to the same
url with trailing slash appended */ url with trailing slash appended */
if (!slash) { if (!slash) {
cl->send_header(cl, 302, "Found", 0); cl->redirect(cl, 302, "%s%s%s", &path_phys[docroot_len], query ? "?" : "", query ? query : "");
cl->printf(cl, "Location: %s%s%s\r\n\r\n", &path_phys[docroot_len],
query ? "?" : "", query ? query : "");
cl->request_done(cl);
p.redirected = 1; p.redirected = 1;
return &p; return &p;
} }