int main(int argc, char* argv[]) { char* ipaddr = "0.0.0.0"; int port = 7000; int i; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-a")) { if (i == argc-1) usage(argv[0]); ipaddr = argv[++i]; } else if (!strcmp(argv[i], "-p")) { if (i == argc-1) usage(argv[0]); char* e = NULL; port = strtol(argv[++i], &e, 10); if ((e && *e) || port < 0 || port > 65535) usage(argv[0]); } else if (!strcmp(argv[i], "-d")) { if (i == argc-1) usage(argv[0]); static_dir = argv[++i]; } else usage(argv[0]); } static_dir_len = strlen(static_dir); struct sockaddr_in addr; int r; mime_type = kh_init(mime_type); add_mime_type(".jpg", "image/jpeg"); add_mime_type(".png", "image/png"); add_mime_type(".gif", "image/gif"); add_mime_type(".html", "text/html"); add_mime_type(".txt", "text/plain"); r = uv_ip4_addr(ipaddr, port, &addr); if (r) { fprintf(stderr, "Address error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } loop = uv_default_loop(); uv_tcp_t server; r = uv_tcp_init(loop, &server); if (r) { fprintf(stderr, "Socket creation error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0); if (r) { fprintf(stderr, "Bind error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } r = uv_tcp_simultaneous_accepts((uv_tcp_t*) &server, 1); if (r) { fprintf(stderr, "Accept error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } fprintf(stderr, "Listening %s:%d\n", ipaddr, port); r = uv_listen((uv_stream_t*)&server, SOMAXCONN, on_connection); if (r) { fprintf(stderr, "Listen error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } uv_signal_t sig; r = uv_signal_init(loop, &sig); if (r) { fprintf(stderr, "Signal error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } sig.data = loop; r = uv_signal_start(&sig, on_signal, SIGINT); if (r) { fprintf(stderr, "Signal error: %s: %s\n", uv_err_name(r), uv_strerror(r)); return 1; } return uv_run(loop, UV_RUN_DEFAULT); }
void init_mime_types() { int i = 0; for ( i = 0; i < 26; i++ ) { mime_types[i] = NULL; } add_mime_type ( "htm", "text/html" ); add_mime_type ( "lua", "text/plain" ); add_mime_type ( "txt", "text/plain" ); add_mime_type ( "php", "text/plain" ); add_mime_type ( "java", "text/plain" ); add_mime_type ( "log", "text/plain" ); add_mime_type ( "css", "text/css" ); add_mime_type ( "js", "text/javascript" ); add_mime_type ( "json", "application/json" ); add_mime_type ( "html", "text/html" ); add_mime_type ( "jpg", "image/jpeg" ); add_mime_type ( "jpeg", "image/jpeg" ); add_mime_type ( "gif", "image/gif" ); add_mime_type ( "png", "image/png" ); add_mime_type ( "ico", "image/icon" ); add_mime_type ( "woff", "application/x-woff" ); add_mime_type ( "ttf", "font/truetype" ); add_mime_type ( "otf", "font/opentype" ); }