static void mimeLoadIconFile(const char *icon) { int fd; int n; request_flags flags; struct stat sb; StoreEntry *e; LOCAL_ARRAY(char, path, MAXPATHLEN); LOCAL_ARRAY(char, url, MAX_URL); char *buf; const char *type = mimeGetContentType(icon); HttpReply *reply; if (type == NULL) fatal("Unknown icon format while reading mime.conf\n"); buf = internalLocalUri("/squid-internal-static/icons/", icon); xstrncpy(url, buf, MAX_URL); if (storeGetPublic(url, METHOD_GET)) return; snprintf(path, MAXPATHLEN, "%s/%s", Config.icons.directory, icon); fd = file_open(path, O_RDONLY); if (fd < 0) { debug(25, 0) ("mimeLoadIconFile: %s: %s\n", path, xstrerror()); return; } if (fstat(fd, &sb) < 0) { debug(50, 0) ("mimeLoadIconFile: FD %d: fstat: %s\n", fd, xstrerror()); return; } flags = null_request_flags; flags.cachable = 1; e = storeCreateEntry(url, url, flags, METHOD_GET); assert(e != NULL); storeSetPublicKey(e); storeBuffer(e); e->mem_obj->request = requestLink(urlParse(METHOD_GET, url)); httpReplyReset(reply = e->mem_obj->reply); httpReplySetHeaders(reply, 1.0, HTTP_OK, NULL, type, (int) sb.st_size, sb.st_mtime, -1); reply->cache_control = httpHdrCcCreate(); httpHdrCcSetMaxAge(reply->cache_control, 86400); httpHeaderPutCc(&reply->header, reply->cache_control); httpReplySwapOut(reply, e); reply->hdr_sz = e->mem_obj->inmem_hi; /* yuk */ /* read the file into the buffer and append it to store */ buf = memAllocate(MEM_4K_BUF); while ((n = read(fd, buf, 4096)) > 0) storeAppend(e, buf, n); file_close(fd); EBIT_SET(e->flags, ENTRY_SPECIAL); storeBufferFlush(e); storeComplete(e); storeTimestampsSet(e); debug(25, 3) ("Loaded icon %s\n", url); storeUnlockObject(e); memFree(buf, MEM_4K_BUF); }
/* create MIME Header for Gopher Data */ static void gopherMimeCreate(GopherStateData * gopherState) { StoreEntry *e = gopherState->entry; HttpReply *reply = e->mem_obj->reply; http_version_t version; const char *mime_type = NULL; const char *mime_enc = NULL; switch (gopherState->type_id) { case GOPHER_DIRECTORY: case GOPHER_INDEX: case GOPHER_HTML: case GOPHER_WWW: case GOPHER_CSO: mime_type = "text/html"; break; case GOPHER_GIF: case GOPHER_IMAGE: case GOPHER_PLUS_IMAGE: mime_type = "image/gif"; break; case GOPHER_SOUND: case GOPHER_PLUS_SOUND: mime_type = "audio/basic"; break; case GOPHER_PLUS_MOVIE: mime_type = "video/mpeg"; break; case GOPHER_MACBINHEX: mime_type = "application/macbinary"; break; case GOPHER_DOSBIN: case GOPHER_UUENCODED: case GOPHER_BIN: case GOPHER_FILE: default: /* Rightnow We have no idea what it is. */ mime_type = mimeGetContentType(gopherState->request); mime_enc = mimeGetContentEncoding(gopherState->request); break; } storeBuffer(e); httpReplyReset(reply); EBIT_CLR(gopherState->entry->flags, ENTRY_FWD_HDR_WAIT); httpBuildVersion(&version, 1, 0); httpReplySetHeaders(reply, version, HTTP_OK, "Gatewaying", mime_type, -1, -1, -1); if (mime_enc) httpHeaderPutStr(&reply->header, HDR_CONTENT_ENCODING, mime_enc); httpReplySwapOut(reply, e); reply->hdr_sz = e->mem_obj->inmem_hi; storeTimestampsSet(e); if (EBIT_TEST(e->flags, ENTRY_CACHABLE)) { storeSetPublicKey(e); } else { storeRelease(e); } }
/* figure out content type from file extension */ static void gopher_mime_content(MemBuf * mb, const char *name, const char *def_ctype) { char *ctype = mimeGetContentType(name); char *cenc = mimeGetContentEncoding(name); if (cenc) memBufPrintf(mb, "Content-Encoding: %s\r\n", cenc); memBufPrintf(mb, "Content-Type: %s\r\n", ctype ? ctype : def_ctype); }
static unsigned short httpReadHeaders(HTTP *http, App *app, Buf *buf, unsigned char *url, unsigned char **ct, int *chunked) { unsigned short c; unsigned char *charset; unsigned char *contentType; int locationFound; unsigned char *name; URL *rel; ContentType *type; unsigned char *value; char *version; app->printHTML(app, "<h4>Response</h4>"); app->printHTML(app, "<pre>"); contentType = NULL; locationFound = 0; bufMark(buf, 0); c = bufGetByte(buf); if (c == 256) { *ct = emptyHTTPResponse; return c; } nonEmptyHTTPResponseCount++; c = readNonWhiteSpace(buf, c); bufMark(buf, -1); app->httpResponse(app, buf); version = (char *) bufCopy(buf); if (!strcmp(version, "HTTP/1.0")) { } else if (!strcmp(version, "HTTP/1.1")) { } else if (!strncmp(version, "HTTP/", 5)) { } else { /* XXX deal with HTTP/0.9? */ *ct = http09Response; return c; } free(version); http10OrGreaterCount++; c = readSpaceTab(buf, c); c = readNumber(buf, c, &http->status); c = readLine(buf, c); while (1) { if (c == 256) { bufMark(buf, 0); app->httpResponse(app, buf); break; } bufMark(buf, -1); app->httpResponse(app, buf); if ((c == '\r') || (c == '\n')) { readLine(buf, c); bufUnGetByte(buf); bufMark(buf, 0); app->httpResponse(app, buf); break; } while ( (c != 256) && (c != '\r') && (c != '\n') && (c != ':') ) { c = bufGetByte(buf); } if (c != ':') { bufMark(buf, -1); fprintf(stderr, "no colon in HTTP header \"%s\": %s\n", bufCopy(buf), url); *ct = NULL; return c; } bufMark(buf, -1); app->httpResponseHeaderName(app, buf); name = bufCopyLower(buf); c = readSpaceTab(buf, bufGetByte(buf)); bufMark(buf, -1); app->httpResponse(app, buf); c = readLine(buf, c); if ((c == ' ') || (c == '\t')) { do { c = readLine(buf, c); } while ((c == ' ') || (c == '\t')); } c = bufTrimTrailingWhiteSpace(buf); bufMark(buf, -1); value = bufCopy(buf); if (!strcasecmp((char *) name, "content-type")) { app->httpResponseHeaderValue(app, buf, NULL); type = mimeParseContentType(value); contentType = mimeGetContentType(type); charset = mimeGetContentTypeParameter(type, "charset"); if (charset) { app->httpResponseCharSet(app, charset); free(charset); } mimeFreeContentType(type); } else if (!strcasecmp((char *) name, "location")) { /* XXX supposed to be absolute URL? */ rel = urlRelative(url, value); addURL(app, rel->url); app->httpResponseHeaderValue(app, buf, rel->url); urlFree(rel); locationFound = 1; } else if (!strcasecmp((char *) name, "transfer-encoding")) { app->httpResponseHeaderValue(app, buf, NULL); if (!strcasecmp((char *) value, "chunked")) { *chunked = 1; } } else { app->httpResponseHeaderValue(app, buf, NULL); } free(name); free(value); c = readLine(buf, c); bufMark(buf, -1); app->httpResponse(app, buf); } if (!contentType) { if (locationFound) { *ct = locationURLWasAdded; return c; } } *ct = contentType; return c; }