#include #include enum{BULLET = 1, TYPE}; char* mime(char *k, int v){ static char *m[] = { "mp4", "⛲", "video/mp4", "it", "♫", "audio/it", "mod", "♫", "audio/mod", "mp3", "♫", "audio/mpeg", "ogg", "♫", "audio/ogg", "gif", "❧", "image/gif", "jpg", "❧", "image/jpeg", "png", "❧", "image/png", "pdf", "⎙", "application/pdf", "ps", "⎙", "application/postscript", "tgz", "✇", "application/gzip", nil, "☞", "text/plain; charset=utf-8" }; int i; k = strrchr(k, '.'); if(k == nil) return m[nelem(m) - 3 + v]; for(k++, i = 0; m[i] != nil && cistrcmp(k, m[i]); i += 3); return m[i + v]; } void e(char *s){ print("HTTP/1.1 %s\r\n" "Content-type: text/html; charset=utf-8\r\n\r\n" "%s" "

%s

", s, s, s); exits(nil); } void dec(char *d, char *s){ for(; *s && *s != ' '; s++){ if(*s == '+') *d++ = ' '; else if(*s == '%'){ if(s[1] >= '0' && s[1] <= '9') *d = s[1] - '0' << 4; else if(s[1] >= 'A' && s[1] <= 'F') *d = s[1] - 'A' + 10 << 4; else if(s[1] >= 'a' && s[1] <= 'f') *d = s[1] - 'a' + 10 << 4; else{*d++ = '%'; continue;} if(s[2] >= '0' && s[2] <= '9') *d++ |= s[2] - '0'; else if(s[2] >= 'A' && s[2] <= 'F') *d++ |= s[2] - 'A' + 10; else if(s[2] >= 'a' && s[2] <= 'f') *d++ |= s[2] - 'a' + 10; else{*d++ = '%'; continue;} s += 2; }else *d++ = *s; } *d = 0; } char* enc(char *d, char *s){ while(*s){ if(strchr("/$-_@.!*'(),", *s) || 'a' <= *s && *s <= 'z' || 'A' <= *s && *s <= 'Z' || '0' <= *s && *s <= '9') *d++ = *s; else if(*s == ' ') *d++ = '+'; else{ *d++ = '%'; *d++ = ((*s >> 4 & 15) > 9 ? 'a' - 10 : '0') + (*s >> 4 & 15); *d++ = ((*s & 15) > 9 ? 'a' - 10 : '0') + (*s & 15); } s++; } *d = 0; return d; } void main(int, char**){ Dir *d; Tm t; char *b, buf[8192]; int fd, n, i; n = read(0, buf, 2048); if(n < 5) e("400 Bad Request"); buf[n] = 0; if(buf[0] != 'G' || buf[1] != 'E' || buf[2] != 'T' || buf[3] != ' ' || buf[4] != '/') e("501 Not Implemented"); strcpy(buf + 3000, "/usr/umbraticus/jimmy/"); dec(buf + 3022, buf + 5); cleanname(buf + 3021); fd = open(buf + 3000, OREAD); if(fd < 0 || (d = dirfstat(fd)) == nil) e("404 Not Found"); if(d->mode & DMDIR){ b = seprint(buf, buf + sizeof(buf), "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n" "jimmy://%s\n" "\n" "\n" "

jimmy://%s

\n", buf + 3022, buf + 3022); free(d); n = dirreadall(fd, &d); close(fd); strcpy(strchr(buf + 3022, 0), "/readme"); if((fd = open(buf + 3000, OREAD)) >= 0){ b = strecpy(b, buf + sizeof(buf), "
");
			while((i = read(fd, b, buf + sizeof(buf) - b)) > 0) b += i;
			b = strecpy(b - (b[-1] == '\n'), buf + sizeof(buf), "
\n"); } b = strecpy(b, buf + sizeof(buf), ""); write(1, buf, b - buf); }else{ print("HTTP/1.1 200 OK\r\nContent-type: %s\r\n\r\n", mime(d->name, TYPE)); while((n = read(fd, buf, sizeof(buf))) > 0) write(1, buf, n); } }