lmhttp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * http_srv.c - simple HTTP "server"
  3. *
  4. * Only implements the simplest GET operation.
  5. *
  6. * usage: http_srv [-f#] [-l] [-d] [port]
  7. *
  8. * Copyright (c) 1994-6 Larry McVoy. Distributed under the FSF GPL with
  9. * additional restriction that results may published only if
  10. * (1) the benchmark is unmodified, and
  11. * (2) the version in the sccsid below is included in the report.
  12. * Other authors: Steve Alexander, sca@sgi.com.
  13. */
  14. char *id = "$Id$\n";
  15. #ifdef sgi
  16. #include <sys/sysmp.h>
  17. #include <sys/syssgi.h>
  18. #endif
  19. #include "bench.h"
  20. #ifdef MAP_FILE
  21. # define MMAP_FLAGS MAP_FILE|MAP_SHARED
  22. #else
  23. # define MMAP_FLAGS MAP_SHARED
  24. #endif
  25. #define MMAPS_BETTER (4<<10) /* mmap is faster for sizes >= this */
  26. #define LOGFILE "/usr/tmp/lmhttp.log"
  27. char *buf;
  28. char *bufs[3];
  29. int pflg, Dflg, dflg, nflg, lflg, fflg, zflg;
  30. int data, logfile;
  31. void die();
  32. void worker();
  33. char *http_time(void);
  34. char *date(time_t *tt);
  35. char *type(char *name);
  36. int source(int sock);
  37. int isdir(char *name);
  38. void dodir(char *name, int sock);
  39. void fake(int sock, char *buf, int size);
  40. void rdwr(int fd, int sock, char *buf);
  41. int mmap_rdwr(int from, int to, int size);
  42. void logit(int sock, char *name, int size);
  43. int
  44. main(int ac, char **av)
  45. {
  46. int i, prog;
  47. #ifdef sgi
  48. int ncpus = sysmp(MP_NPROCS);
  49. #endif
  50. for (i = 1; i < ac; ++i) {
  51. if (av[i][0] != '-') {
  52. break;
  53. }
  54. switch (av[i][1]) {
  55. case 'D': Dflg = 1; break; /* Allow directories */
  56. case 'd': dflg = 1; break; /* debugging */
  57. case 'f': fflg = atoi(&av[i][2]);
  58. break; /* # of threads */
  59. case 'l': lflg = 1; break; /* logging */
  60. case 'n': nflg = 1; break; /* fake file i/o */
  61. case 'p': pflg = 1; break; /* pin them */
  62. case 'z': zflg = 1; break; /* all files are 0 size */
  63. default:
  64. fprintf(stderr, "Barf.\n");
  65. exit(1);
  66. }
  67. }
  68. if (getenv("DOCROOT")) {
  69. if (chdir(getenv("DOCROOT")) == -1) {
  70. perror(getenv("DOCROOT"));
  71. exit(1);
  72. }
  73. }
  74. if (atoi(av[ac - 1]) != 0) {
  75. prog = -atoi(av[ac - 1]);
  76. } else {
  77. prog = -80;
  78. }
  79. /*
  80. * Steve - why is this here?
  81. */
  82. signal(SIGPIPE, SIG_IGN);
  83. data = tcp_server(prog, SOCKOPT_REUSE);
  84. bufs[0] = valloc(XFERSIZE);
  85. bufs[1] = valloc(XFERSIZE);
  86. bufs[2] = valloc(XFERSIZE);
  87. logfile = open(LOGFILE, O_CREAT|O_APPEND|O_WRONLY, 0666);
  88. signal(SIGINT, die);
  89. signal(SIGHUP, die);
  90. signal(SIGTERM, die);
  91. for (i = 1; i < fflg; ++i) {
  92. if (fork() <= 0) {
  93. #ifdef sgi
  94. if (pflg) sysmp(MP_MUSTRUN, i % ncpus);
  95. #endif
  96. break;
  97. }
  98. }
  99. #ifdef sgi
  100. if (pflg) sysmp(MP_MUSTRUN, i % ncpus);
  101. #endif
  102. worker();
  103. return(0);
  104. }
  105. void
  106. worker()
  107. {
  108. int newdata;
  109. int next = 0;
  110. for (;;) {
  111. buf = bufs[next];
  112. if (++next == 3) next = 0;
  113. newdata = tcp_accept(data, SOCKOPT_REUSE);
  114. source(newdata);
  115. close(newdata);
  116. }
  117. }
  118. /*
  119. * "Tue, 28 Jan 97 01:20:30 GMT";
  120. * 012345678901234567890123456
  121. */
  122. char *http_time()
  123. {
  124. time_t tt;
  125. static time_t save_tt;
  126. struct tm *t;
  127. static struct tm save_tm;
  128. static char buf[100];
  129. time(&tt); /* costs 10 usecs */
  130. if (tt == save_tt) {
  131. return (buf);
  132. }
  133. save_tt = tt;
  134. t = gmtime(&tt); /* costs 21 usecs */
  135. if (buf[0] && (tt - save_tt < 3600)) {
  136. buf[22] = t->tm_sec / 10 + '0';
  137. buf[21] = t->tm_sec % 10 + '0';
  138. save_tm.tm_sec = t->tm_sec;
  139. if (save_tm.tm_min == t->tm_min) {
  140. return (buf);
  141. }
  142. }
  143. save_tm = *t;
  144. /* costs 120 usecs */
  145. strftime(buf, sizeof(buf), "%a, %d %b %y %H:%M:%S %Z", t);
  146. return(buf);
  147. }
  148. /*
  149. * Input: dates that are probably within the last year.
  150. * Output: Tue, 28 Jan 97 01:20:30 GMT
  151. *
  152. * Since it costs 150 usecs or so to do this on an Indy, it may pay to
  153. * optimize this.
  154. */
  155. char *
  156. date(time_t *tt)
  157. {
  158. return "Tue, 28 Jan 97 01:20:30 GMT";
  159. }
  160. char *
  161. type(char *name)
  162. {
  163. int len = strlen(name);
  164. if (!strcmp(&name[len - 4], ".gif")) {
  165. return "image/gif";
  166. }
  167. if (!strcmp(&name[len - 5], ".jpeg")) {
  168. return "image/jpeg";
  169. }
  170. if (!strcmp(&name[len - 5], ".html")) {
  171. return "text/html";
  172. }
  173. if (Dflg && isdir(name)) {
  174. return "text/html";
  175. }
  176. return "text/plain";
  177. }
  178. /*
  179. * Read the file to be transfered.
  180. * Write that file on the data socket.
  181. * The caller closes the socket.
  182. */
  183. int
  184. source(int sock)
  185. {
  186. int fd, n, size;
  187. char *s;
  188. char file[100];
  189. char hbuf[1024];
  190. struct stat sb;
  191. #define name &buf[5]
  192. n = read(sock, buf, XFERSIZE);
  193. if (n <= 0) {
  194. perror("control nbytes");
  195. return (-1);
  196. }
  197. buf[n] = 0;
  198. if (dflg) printf("%.*s\n", n, buf);
  199. if (zflg) {
  200. return (0);
  201. }
  202. if (!strncmp(buf, "EXIT", 4)) {
  203. exit(0);
  204. }
  205. if (strncmp(buf, "GET /", 5)) {
  206. perror(buf);
  207. return(1);
  208. }
  209. for (s = buf; *s && *s != '\r' && *s != '\n'; s++)
  210. ;
  211. *s = 0;
  212. for (s = name; *s && *s != ' '; s++)
  213. ;
  214. *s = 0;
  215. if (lflg) strncpy(file, name, sizeof(file));
  216. if (dflg) printf("OPEN %s\n", name);
  217. fd = open(name, 0);
  218. if (fd == -1) {
  219. error: perror(name);
  220. close(fd);
  221. return (1);
  222. }
  223. if (fstat(fd, &sb) == -1) {
  224. if (dflg) printf("Couldn't stat %s\n", name);
  225. goto error;
  226. }
  227. size = sb.st_size;
  228. n = sprintf(hbuf, "HTTP/1.0 200 OK\r\n%s\r\nServer: lmhttp/0.1\r\nContent-Type: %s\r\nLast-Modified: %s\r\n\r\n",
  229. http_time(), type(name), date(&sb.st_mtime));
  230. if (write(sock, hbuf, n) != n) {
  231. goto error;
  232. }
  233. if (Dflg && isdir(name)) {
  234. dodir(name, sock);
  235. } else if (nflg) {
  236. fake(sock, buf, size);
  237. } else if ((size > MMAPS_BETTER)) { /* XXX */
  238. if (mmap_rdwr(fd, sock, size) == -1) {
  239. printf("%s mmap failed\n", name);
  240. }
  241. } else {
  242. rdwr(fd, sock, buf);
  243. }
  244. if (lflg) logit(sock, file, size);
  245. close(fd);
  246. return(0);
  247. }
  248. #undef name
  249. int
  250. isdir(char *name)
  251. {
  252. struct stat sb;
  253. if (stat(name, &sb) == -1) {
  254. return(0);
  255. }
  256. return (S_ISDIR(sb.st_mode));
  257. }
  258. #ifdef example
  259. <HTML><HEAD>
  260. <TITLE>Index of /pub/Linux</TITLE>
  261. </HEAD><BODY>
  262. <H1>Index of /pub/Linux</H1>
  263. <PRE><IMG SRC="/icons/blank.gif" ALT=" "> Name Last modified Size Description
  264. <HR>
  265. <IMG SRC="/icons/unknown.gif" ALT="[ ]"> <A HREF="!INDEX">!INDEX</A> 19-Sep-97 03:20 3k
  266. <IMG SRC="/icons/text.gif" ALT="[TXT]"> <A HREF="!INDEX.html">!INDEX.html</A> 19-Sep-97 03:20 6k
  267. #endif
  268. void
  269. dodir(char *name, int sock)
  270. {
  271. FILE *p;
  272. char buf[1024];
  273. char path[1024];
  274. if (dflg) printf("dodir(%s)\n", name);
  275. sprintf(buf, "cd %s && ls -1a", name);
  276. p = popen(buf, "r");
  277. if (!p && dflg) printf("Couldn't popen %s\n", buf);
  278. sprintf(buf, "\
  279. <HTML><HEAD>\n<TITLE>Index of /%s</TITLE></HEAD><BODY><H1>Index of /%s</H1>\n",
  280. name, name);
  281. write(sock, buf, strlen(buf));
  282. while (fgets(buf, sizeof(buf), p)) {
  283. buf[strlen(buf) - 1] = 0;
  284. sprintf(path, "/%s/%s", name, buf);
  285. if (dflg) printf("\t%s\n", path);
  286. write(sock, "<A HREF=\"", 9);
  287. write(sock, path, strlen(path));
  288. write(sock, "\">", 2);
  289. write(sock, buf, strlen(buf));
  290. write(sock, "</A><BR>\n", 9);
  291. }
  292. pclose(p);
  293. }
  294. void
  295. fake(int sock, char *buf, int size)
  296. {
  297. int n;
  298. while (size > 0) {
  299. n = write(sock, buf, size > XFERSIZE ? XFERSIZE : size);
  300. if (n == -1) {
  301. perror("write on socket");
  302. return;
  303. }
  304. size -= n;
  305. }
  306. }
  307. void
  308. rdwr(int fd, int sock, char *buf)
  309. {
  310. int nread;
  311. while ((nread = read(fd, buf, XFERSIZE)) > 0) {
  312. int i;
  313. for (i = 0; i < nread; ) {
  314. int nwrote = write(sock, buf, nread - i);
  315. if (i < 0) {
  316. exit(1);
  317. }
  318. i += nwrote;
  319. }
  320. }
  321. }
  322. int
  323. mmap_rdwr(int from, int to, int size)
  324. {
  325. char *buf;
  326. int done = 0, wrote;
  327. buf = mmap(0, size, PROT_READ, MMAP_FLAGS, from, 0);
  328. if ((int)buf == -1) {
  329. perror("mmap");
  330. return (-1);
  331. }
  332. do {
  333. wrote = write(to, buf + done, size - done);
  334. if (wrote == -1) {
  335. perror("write");
  336. break;
  337. }
  338. done += wrote;
  339. } while (done < size);
  340. if (munmap(buf, size) == -1) {
  341. perror("unmap");
  342. }
  343. return (0);
  344. }
  345. static char logbuf[64<<10]; /* buffer into here */
  346. static int nbytes; /* bytes buffered */
  347. /*
  348. * HTTP server logging, compressed format.
  349. */
  350. void
  351. logit(int sock, char *name, int size)
  352. {
  353. struct sockaddr_in sin;
  354. int len = sizeof(sin);
  355. char buf[1024 + 16]; /* maxpathlen + others */
  356. if (getpeername(sock, (struct sockaddr*)&sin, &len) == -1) {
  357. perror("getpeername");
  358. return;
  359. }
  360. len = sprintf(buf, "%u %u %s %u\n",
  361. *((unsigned int*)&sin.sin_addr), (unsigned int)time(0), name, size);
  362. if (nbytes + len >= sizeof(logbuf)) {
  363. write(logfile, logbuf, nbytes);
  364. nbytes = 0;
  365. }
  366. bcopy(buf, &logbuf[nbytes], len);
  367. nbytes += len;
  368. }
  369. void die()
  370. {
  371. if (nbytes) {
  372. write(logfile, logbuf, nbytes);
  373. nbytes = 0;
  374. }
  375. exit(1);
  376. }