void test_options_file() { printf("Testing configurations file\n"); /* *Write sample entries in a test file. */ char entries_file[] = "desktop_entry.sample"; const char *keys[5] = {"text/html", "application/json+xml", "application/xhtml", "x-scheme-handler/http", "text/javascript"}; FILE *fp; if(!(fp = fopen(entries_file, "w"))) { perror("test_desktop_entries_file"); exit(1); } fprintf(fp, "[Title, or section label?]\n"); fprintf(fp, "something=in_their_language\n"); fprintf(fp, "settings[attr]=value or something\n"); fprintf(fp, "MimeType=text/html;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/geo;image/svg+xml;"); fprintf(fp, "settings[attr]=value or something\n"); fclose(fp); /* *Now test if the file was successfully parsed. */ assert(mime_lookup(entries_file, keys, 5) == 4); printf("ALL TESTS PASSED!\n"); }
const char *type_for_ext(const char *ext) { struct mimetype *tuple; if ((tuple = mime_lookup(ext, strlen(ext))) != NULL) { return tuple->pmimetype; } return NULL; }
char *mime_auto(char *file_path) { char *extension; /* search for the last . in the file_path, if it is present, go to it, and then start searching the tree to find the right mime */ extension = strrchr(file_path, '.'); extension++; return mime_lookup(extension); }
/** serve_page - serve a page via HTTP * @param url requested * @param client to serve to * @param data - docroot */ gboolean serve_page(MediaURL *url, GstHTTPClient *client, gpointer data) { int fd; char buf[1024]; int sz; struct stat sb; const char *docroot = (const char*) data; gchar *path; char *physpath; const char *mimetype; if (strcmp(url->path, "/") == 0) path = g_strconcat(docroot, url->path, "index.html", NULL); else path = g_strconcat(docroot, url->path, NULL); physpath = realpath(path, NULL); if (!physpath) goto err; /* ensure physpath is within docroot */ if (strncmp(physpath, docroot, strlen(docroot)) || ((physpath[strlen(docroot)] != 0) && (physpath[strlen(docroot)] != '/')) ) { goto err; } /* ensure file is readable */ if ( (stat(physpath, &sb) < 0) || (fd = open(physpath, O_RDONLY)) == -1) { goto err; } if (!S_ISREG(sb.st_mode)) { close(fd); goto err; } /* obtain mimetype */ mimetype = mime_lookup(physpath); GST_INFO("Serving %d byte %s to %s:%d as %s", (int)sb.st_size, physpath, client->peer_ip, client->port, mimetype); WRITELN(client, "Last-Modified: %s", unix2date(sb.st_mtime)); WRITELN(client, "Content-Length: %ld", sb.st_size); WRITELN(client, "Content-Type: %s\r\n", mimetype); while ( (sz = read(fd, buf, sizeof(buf))) > 0) { write(client->sock, buf, sz); } write(client->sock, "", 0); close(fd); free(physpath); g_free(path); return TRUE; err: GST_ERROR("404 Not Found: %s", path); if (physpath) free(physpath); g_free(path); return TRUE; }