From: Amos Jeffries Date: Tue, 31 Dec 2013 13:55:12 +0000 (-0800) Subject: Rename mime_get_header_field() as Http1Parser::getHeaderField() X-Git-Tag: merge-candidate-3-v1~506^2~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4181565e6017fa250bca7a67b3fdf778bfbd390;p=thirdparty%2Fsquid.git Rename mime_get_header_field() as Http1Parser::getHeaderField() --- diff --git a/src/client_side.cc b/src/client_side.cc index 1f67c91e57..10fa4e961d 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -2080,7 +2080,7 @@ setLogUri(ClientHttpRequest * http, char const *uri, bool cleanUrl) } static void -prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const char *req_hdr) +prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const Http::Http1ParserPointer &hp) { int vhost = conn->port->vhost; int vport = conn->port->vport; @@ -2121,7 +2121,7 @@ prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const bool switchedToHttps = conn->switchedToHttps(); const bool tryHostHeader = vhost || switchedToHttps; - if (tryHostHeader && (host = mime_get_header_field(req_hdr, "Host")) != NULL) { + if (tryHostHeader && (host = hp->getHeaderField("Host")) != NULL) { debugs(33, 5, "ACCEL VHOST REWRITE: vhost=" << host << " + vport=" << vport); char thost[256]; if (vport > 0) { @@ -2170,7 +2170,7 @@ prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url, } static void -prepareTransparentURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const char *req_hdr) +prepareTransparentURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const Http::Http1ParserPointer &hp) { char *host; char ipbuf[MAX_IPSTRLEN]; @@ -2180,7 +2180,7 @@ prepareTransparentURL(ConnStateData * conn, ClientHttpRequest *http, char *url, /* BUG: Squid cannot deal with '*' URLs (RFC2616 5.1.2) */ - if ((host = mime_get_header_field(req_hdr, "Host")) != NULL) { + if ((host = hp->getHeaderField("Host")) != NULL) { int url_sz = strlen(url) + 32 + Config.appendDomainLen + strlen(host); http->uri = (char *)xcalloc(url_sz, 1); @@ -2278,11 +2278,7 @@ parseHttpRequest(ConnStateData *csd, const Http::Http1ParserPointer &hp) * Process headers after request line * TODO: Use httpRequestParse here. */ - /* XXX this code should be modified to take a const char * later! */ - const char *req_hdr = hp->rawHeaderBuf(); - - debugs(33, 3, Raw("req_hdr", req_hdr, hp->headerBlockSize())); - + debugs(33, 3, Raw("req_hdr", hp->rawHeaderBuf(), hp->headerBlockSize())); debugs(33, 3, "prefix_sz = " << hp->messageHeaderSize() << ", request-line-size=" << hp->firstLineSize() << ", mime-header-size=" << hp->headerBlockSize()); @@ -2336,7 +2332,7 @@ parseHttpRequest(ConnStateData *csd, const Http::Http1ParserPointer &hp) */ if (csd->transparent()) { /* intercept or transparent mode, properly working with no failures */ - prepareTransparentURL(csd, http, url, req_hdr); + prepareTransparentURL(csd, http, url, hp); } else if (internalCheck(url)) { /* internal URL mode */ @@ -2348,7 +2344,7 @@ parseHttpRequest(ConnStateData *csd, const Http::Http1ParserPointer &hp) } else if (csd->port->flags.accelSurrogate || csd->switchedToHttps()) { /* accelerator mode */ - prepareAcceleratedURL(csd, http, url, req_hdr); + prepareAcceleratedURL(csd, http, url, hp); } if (!http->uri) { diff --git a/src/http/Http1Parser.cc b/src/http/Http1Parser.cc index b7041fa0c9..5e36d1f752 100644 --- a/src/http/Http1Parser.cc +++ b/src/http/Http1Parser.cc @@ -370,3 +370,65 @@ Http::Http1Parser::parseRequest() return isDone(); } + +// arbitrary maximum-length for headers which can be found by Http1Parser::getHeaderField() +#define GET_HDR_SZ 1024 + +char * +Http::Http1Parser::getHeaderField(const char *name) +{ + LOCAL_ARRAY(char, header, GET_HDR_SZ); + const char *p = NULL; + char *q = NULL; + char got = 0; + const int namelen = name ? strlen(name) : 0; + + if (!headerBlockSize() || !name) + return NULL; + + debugs(25, 5, "looking for '" << name << "'"); + + for (p = rawHeaderBuf(); *p; p += strcspn(p, "\n\r")) { + if (strcmp(p, "\r\n\r\n") == 0 || strcmp(p, "\n\n") == 0) + return NULL; + + while (xisspace(*p)) + ++p; + + if (strncasecmp(p, name, namelen)) + continue; + + if (!xisspace(p[namelen]) && p[namelen] != ':') + continue; + + int l = strcspn(p, "\n\r") + 1; + + if (l > GET_HDR_SZ) + l = GET_HDR_SZ; + + xstrncpy(header, p, l); + + debugs(25, 5, "checking '" << header << "'"); + + q = header; + + q += namelen; + + if (*q == ':') { + ++q; + got = 1; + } + + while (xisspace(*q)) { + ++q; + got = 1; + } + + if (got) { + debugs(25, 5, "returning '" << q << "'"); + return q; + } + } + + return NULL; +} diff --git a/src/http/Http1Parser.h b/src/http/Http1Parser.h index b217edfdc7..803b5b82e8 100644 --- a/src/http/Http1Parser.h +++ b/src/http/Http1Parser.h @@ -74,6 +74,11 @@ public: */ bool parseRequest(); + /** + * \return A pointer to a field-value of the first matching field-name, or NULL. + */ + char *getHeaderField(const char *name); + public: const char *buf; int bufsiz; diff --git a/src/mime.cc b/src/mime.cc index e0c2983f54..d116b61a76 100644 --- a/src/mime.cc +++ b/src/mime.cc @@ -51,8 +51,6 @@ #include #endif -#define GET_HDR_SZ 1024 - /* forward declarations */ static void mimeFreeMemory(void); static char const *mimeGetIcon(const char *fn); diff --git a/src/mime_header.cc b/src/mime_header.cc index 2347481171..2a40576485 100644 --- a/src/mime_header.cc +++ b/src/mime_header.cc @@ -31,77 +31,9 @@ */ #include "squid.h" - -#define GET_HDR_SZ 1024 #include "Debug.h" #include "profiler/Profiler.h" -/* - * returns a pointer to a field-value of the first matching field-name where - * field-value matches prefix if any - */ -char * -mime_get_header_field(const char *mime, const char *name) -{ - LOCAL_ARRAY(char, header, GET_HDR_SZ); - const char *p = NULL; - char *q = NULL; - char got = 0; - const int namelen = name ? strlen(name) : 0; - int l; - - if (NULL == mime) - return NULL; - - assert(NULL != name); - - debugs(25, 5, "mime_get_header: looking for '" << name << "'"); - - for (p = mime; *p; p += strcspn(p, "\n\r")) { - if (strcmp(p, "\r\n\r\n") == 0 || strcmp(p, "\n\n") == 0) - return NULL; - - while (xisspace(*p)) - ++p; - - if (strncasecmp(p, name, namelen)) - continue; - - if (!xisspace(p[namelen]) && p[namelen] != ':') - continue; - - l = strcspn(p, "\n\r") + 1; - - if (l > GET_HDR_SZ) - l = GET_HDR_SZ; - - xstrncpy(header, p, l); - - debugs(25, 5, "mime_get_header: checking '" << header << "'"); - - q = header; - - q += namelen; - - if (*q == ':') { - ++q; - got = 1; - } - - while (xisspace(*q)) { - ++q; - got = 1; - } - - if (got) { - debugs(25, 5, "mime_get_header: returning '" << q << "'"); - return q; - } - } - - return NULL; -} - size_t headersEnd(const char *mime, size_t l) { diff --git a/src/mime_header.h b/src/mime_header.h index 3fd272bf18..1a9a5f6b3e 100644 --- a/src/mime_header.h +++ b/src/mime_header.h @@ -33,7 +33,6 @@ #ifndef SQUID_MIME_HEADER_H_ #define SQUID_MIME_HEADER_H_ -char *mime_get_header_field(const char *mime, const char *name); size_t headersEnd(const char *, size_t); #endif /* SQUID_MIME_HEADER_H_ */