redirected.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bin.h>
  4. #include <httpd.h>
  5. int
  6. hredirected(HConnect *c, char *how, char *uri)
  7. {
  8. Hio *hout;
  9. char *s, *ss, *host;
  10. int n;
  11. host = c->head.host;
  12. if(strchr(uri, ':')){
  13. host = "";
  14. }else if(uri[0] != '/'){
  15. s = strrchr(c->req.uri, '/');
  16. if(s != nil)
  17. *s = '\0';
  18. ss = halloc(c, strlen(c->req.uri) + strlen(uri) + 2 + UTFmax);
  19. sprint(ss, "%s/%s", c->req.uri, uri);
  20. uri = ss;
  21. if(s != nil)
  22. *s = '/';
  23. }
  24. n = snprint(c->xferbuf, HBufSize,
  25. "<head><title>Redirection</title></head>\r\n"
  26. "<body><h1>Redirection</h1>\r\n"
  27. "Your selection can be found <a href=\"%U\"> here</a>.<p></body>\r\n", uri);
  28. hout = &c->hout;
  29. hprint(hout, "%s %s\r\n", hversion, how);
  30. hprint(hout, "Date: %D\r\n", time(nil));
  31. hprint(hout, "Server: Plan9\r\n");
  32. hprint(hout, "Content-type: text/html\r\n");
  33. hprint(hout, "Content-Length: %d\r\n", n);
  34. if(host == nil || host[0] == 0)
  35. hprint(hout, "Location: %U\r\n", uri);
  36. else
  37. hprint(hout, "Location: http://%U%U\r\n", host, uri);
  38. if(c->head.closeit)
  39. hprint(hout, "Connection: close\r\n");
  40. else if(!http11(c))
  41. hprint(hout, "Connection: Keep-Alive\r\n");
  42. hprint(hout, "\r\n");
  43. if(strcmp(c->req.meth, "HEAD") != 0)
  44. hwrite(hout, c->xferbuf, n);
  45. if(c->replog){
  46. if(host == nil || host[0] == 0)
  47. c->replog(c, "Reply: %s\nRedirect: %U\n", how, uri);
  48. else
  49. c->replog(c, "Reply: %s\nRedirect: http://%U%U\n", how, host, uri);
  50. }
  51. return hflush(hout);
  52. }
  53. int
  54. hmoved(HConnect *c, char *uri)
  55. {
  56. return hredirected(c, "301 Moved Permanently", uri);
  57. }