init.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "httpd.h"
  4. #include "httpsrv.h"
  5. void
  6. usage(void)
  7. {
  8. fprint(2, "usage: httpd [-b inbuf] [-d domain] [-r remoteip] [-w webroot] [-N netdir] [-R reqline] [-L logfd0 logfd1] method version uri [search]\n");
  9. exits("usage");
  10. }
  11. char *netdir;
  12. char *webroot;
  13. char *HTTPLOG = "httpd/log";
  14. static HConnect connect;
  15. static HSPriv priv;
  16. HConnect*
  17. init(int argc, char **argv)
  18. {
  19. char *s, *vs;
  20. hinit(&connect.hin, 0, Hread);
  21. hinit(&connect.hout, 1, Hwrite);
  22. hmydomain = nil;
  23. connect.replog = writelog;
  24. connect.private = &priv;
  25. priv.remotesys = nil;
  26. priv.remoteserv = nil;
  27. fmtinstall('D', hdatefmt);
  28. fmtinstall('H', httpfmt);
  29. fmtinstall('U', hurlfmt);
  30. netdir = "/net";
  31. ARGBEGIN{
  32. case 'b':
  33. s = ARGF();
  34. if(s != nil)
  35. hload(&connect.hin, s);
  36. break;
  37. case 'd':
  38. hmydomain = ARGF();
  39. break;
  40. case 'r':
  41. priv.remotesys = ARGF();
  42. break;
  43. case 'w':
  44. webroot = ARGF();
  45. break;
  46. case 'N':
  47. netdir = ARGF();
  48. break;
  49. case 'L':
  50. s = ARGF();
  51. if(s == nil)
  52. usage();
  53. logall[0] = strtol(s, nil, 10);
  54. s = ARGF();
  55. if(s == nil)
  56. usage();
  57. logall[1] = strtol(s, nil, 10);
  58. break;
  59. case 'R':
  60. s = ARGF();
  61. if(s == nil)
  62. usage();
  63. snprint((char*)connect.header, sizeof(connect.header), "%s", s);
  64. break;
  65. default:
  66. usage();
  67. }ARGEND
  68. if(priv.remotesys == nil)
  69. priv.remotesys = "unknown";
  70. if(priv.remoteserv == nil)
  71. priv.remoteserv = "unknown";
  72. if(hmydomain == nil)
  73. hmydomain = "unknown";
  74. if(webroot == nil)
  75. webroot = "/usr/web";
  76. /*
  77. * open all files we might need before castrating namespace
  78. */
  79. time(nil);
  80. syslog(0, HTTPLOG, nil);
  81. if(argc != 4 && argc != 3)
  82. usage();
  83. connect.req.meth = argv[0];
  84. vs = argv[1];
  85. connect.req.vermaj = 0;
  86. connect.req.vermin = 9;
  87. if(strncmp(vs, "HTTP/", 5) == 0){
  88. vs += 5;
  89. connect.req.vermaj = strtoul(vs, &vs, 10);
  90. if(*vs == '.')
  91. vs++;
  92. connect.req.vermin = strtoul(vs, &vs, 10);
  93. }
  94. connect.req.uri = argv[2];
  95. connect.req.search = argv[3];
  96. connect.head.closeit = 1;
  97. connect.hpos = (uchar*)strchr((char*)connect.header, '\0');
  98. connect.hstop = connect.hpos;
  99. connect.reqtime = time(nil); /* not quite right, but close enough */
  100. return &connect;
  101. }