]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Making HttpRequest more class-like. Removed some non-class functions such
authorwessels <>
Fri, 20 Jan 2006 01:40:28 +0000 (01:40 +0000)
committerwessels <>
Fri, 20 Jan 2006 01:40:28 +0000 (01:40 +0000)
as requestCreate() and requestDestroy() and fixed up the clean/reset mess.

src/HttpRequest.cc
src/HttpRequest.h
src/client_side_reply.cc
src/htcp.cc
src/http.cc
src/icp_v2.cc
src/icp_v3.cc
src/url.cc

index 65a06393cf3c8d3107e5f2ebbdf9618fbee9c443..79f0e773a281633cd0c4b5561b4b28e1107a2a6b 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: HttpRequest.cc,v 1.56 2006/01/09 19:59:37 wessels Exp $
+ * $Id: HttpRequest.cc,v 1.57 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 73    HTTP Request
  * AUTHOR: Duane Wessels
 #include "HttpHeaderRange.h"
 #include "MemBuf.h"
 
-HttpRequest::HttpRequest()  : HttpMsg(hoRequest)
+HttpRequest::HttpRequest() : HttpMsg(hoRequest), link_count(0)
 {
+    init();
+}
+
+HttpRequest::HttpRequest(method_t aMethod, protocol_t aProtocol, const char *aUrlpath) : HttpMsg(hoRequest),link_count(0)
+{
+    init();
+    initXX(aMethod, aProtocol, aUrlpath);
+}
+
+HttpRequest::~HttpRequest()
+{
+    assert(link_count == 0);
+    clean();
+}
+
+void
+HttpRequest::initXX(method_t aMethod, protocol_t aProtocol, const char *aUrlpath)
+{
+    method = aMethod;
+    protocol = aProtocol;
+    urlpath = aUrlpath;
+}
+
+void
+HttpRequest::init()
+{
+    /*
+     * NOTE: init() and clean() do NOT touch link_count!
+     */
     method = METHOD_NONE;
+    protocol = PROTO_NONE;
+    urlpath = NULL;
     login[0] = '\0';
     host[0] = '\0';
     auth_user_request = NULL;
     port = 0;
     canonical = NULL;
-    link_count = 0;
     memset(&flags, '\0', sizeof(flags));
     range = NULL;
     ims = -1;
     imslen = 0;
-    max_forwards = 0;
-    client_addr.s_addr = no_addr.s_addr;
-    my_addr.s_addr = no_addr.s_addr;
+    lastmod = -1;
+    max_forwards = -1;
+    client_addr = no_addr;
+    my_addr = no_addr;
     my_port = 0;
     client_port = 0;
     body_connection = NULL;
     // hier
     errType = ERR_NONE;
-    peer_login = NULL;
-    lastmod = -1;
+    peer_login = NULL;         // not allocated/deallocated by this class
+    peer_domain = NULL;                // not allocated/deallocated by this class
     vary_headers = NULL;
-    peer_domain = NULL;
-    tag = "";
-    extacl_user = "";
-    extacl_passwd = "";
-    extacl_log = "";
-}
-
-HttpRequest *
-requestCreate(method_t method, protocol_t protocol, const char *aUrlpath)
-{
-    HttpRequest *req = new HttpRequest;
-    req->method = method;
-    req->protocol = protocol;
-
-    if (aUrlpath)
-        req->urlpath = aUrlpath;
-
-    req->max_forwards = -1;
-
-    req->lastmod = -1;
-
-    req->client_addr = no_addr;
-
-    req->my_addr = no_addr;
-
-    return req;
-}
-
-void HttpRequest::reset()
-{
-    int lc = link_count;
-    clean();
-    *this = HttpRequest(); // XXX: ugly; merge with clean()
-    link_count = lc;
+    tag = null_string;
+    extacl_user = null_string;
+    extacl_passwd = null_string;
+    extacl_log = null_string;
 }
 
 void
-requestDestroy(HttpRequest * req)
+HttpRequest::clean()
 {
-    assert(req);
-    req->clean();
-    delete req;
-}
+    /*
+     * NOTE: init() and clean() do NOT touch link_count!
+     */
 
-// note: this is a very low-level method that leaves us in inconsistent state
-// suitable for deletion or assignment only; XXX: should be merged with reset()
-void HttpRequest::clean()
-{
     if (body_connection.getRaw() != NULL)
         fatal ("request being destroyed with body connection intact\n");
 
-    if (auth_user_request)
+    if (auth_user_request) {
         auth_user_request->unlock();
+        auth_user_request = NULL;
+    }
 
     safe_free(canonical);
 
@@ -131,8 +129,10 @@ void HttpRequest::clean()
         cache_control = NULL;
     }
 
-    if (range)
+    if (range) {
         delete range;
+        range = NULL;
+    }
 
     tag.clean();
 
@@ -143,6 +143,13 @@ void HttpRequest::clean()
     extacl_log.clean();
 }
 
+void
+HttpRequest::reset()
+{
+    clean();
+    init();
+}
+
 bool HttpRequest::sanityCheckStartLine(MemBuf *buf, http_status *error)
 {
     /*
@@ -205,7 +212,6 @@ bool HttpRequest::parseFirstLine(const char *start, const char *end)
     return true;
 }
 
-
 HttpRequest *
 requestLink(HttpRequest * request)
 {
@@ -225,7 +231,7 @@ requestUnlink(HttpRequest * request)
     if (--request->link_count > 0)
         return;
 
-    requestDestroy(request);
+    delete request;
 }
 
 int
@@ -246,49 +252,47 @@ HttpRequest::parseHeader(const char *parse_start)
 
 /* swaps out request using httpRequestPack */
 void
-httpRequestSwapOut(const HttpRequest * req, StoreEntry * e)
+HttpRequest::swapOut(StoreEntry * e)
 {
     Packer p;
-    assert(req && e);
+    assert(e);
     packerToStoreInit(&p, e);
-    httpRequestPack(req, &p);
+    pack(&p);
     packerClean(&p);
 }
 
 /* packs request-line and headers, appends <crlf> terminator */
 void
-httpRequestPack(const HttpRequest * req, Packer * p)
+HttpRequest::pack(Packer * p)
 {
-    assert(req && p);
+    assert(p);
     /* pack request-line */
     packerPrintf(p, "%s %s HTTP/1.0\r\n",
-                 RequestMethodStr[req->method], req->urlpath.buf());
+                 RequestMethodStr[method], urlpath.buf());
     /* headers */
-    httpHeaderPackInto(&req->header, p);
+    httpHeaderPackInto(&header, p);
     /* trailer */
     packerAppend(p, "\r\n", 2);
 }
 
-#if UNUSED_CODE
+/*
+ * A wrapper for debugObj()
+ */
 void
-httpRequestSetHeaders(HttpRequest * req, method_t method, const char *uri, const char *header_str)
+httpRequestPack(void *obj, Packer *p)
 {
-    assert(req && uri && header_str);
-    assert(!req->header.len);
-    httpHeaderParse(&req->header, header_str, header_str + strlen(header_str));
+    HttpRequest *request = static_cast<HttpRequest*>(obj);
+    request->pack(p);
 }
 
-#endif
-
 /* returns the length of request line + headers + crlf */
 int
-httpRequestPrefixLen(const HttpRequest * req)
+HttpRequest::prefixLen()
 {
-    assert(req);
-    return strlen(RequestMethodStr[req->method]) + 1 +
-           req->urlpath.size() + 1 +
+    return strlen(RequestMethodStr[method]) + 1 +
+           urlpath.size() + 1 +
            4 + 1 + 3 + 2 +
-           req->header.len + 2;
+           header.len + 2;
 }
 
 /*
index 0275fa4812e95ed489baa0c6a825d2b499c2128e..00781e267de1e1454f4c6995001e50d36104fc18 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: HttpRequest.h,v 1.15 2005/11/21 22:50:16 wessels Exp $
+ * $Id: HttpRequest.h,v 1.16 2006/01/19 18:40:28 wessels Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
 #include "HierarchyLogEntry.h"
 
 /*  Http Request */
-extern HttpRequest *requestCreate(method_t, protocol_t, const char *urlpath);
-extern void requestDestroy(HttpRequest *);
 extern HttpRequest *requestLink(HttpRequest *);
 extern void requestUnlink(HttpRequest *);
-extern void httpRequestSwapOut(const HttpRequest * req, StoreEntry * e);
-extern void httpRequestPack(const HttpRequest * req, Packer * p);
-extern int httpRequestPrefixLen(const HttpRequest * req);
 extern int httpRequestHdrAllowed(const HttpHeaderEntry * e, String * strConnection);
 extern int httpRequestHdrAllowedByName(http_hdr_type id);
+extern void httpRequestPack(void *obj, Packer *p);
+
 
 class HttpHdrRange;
 
@@ -57,11 +54,19 @@ class HttpRequest: public HttpMsg
 public:
     MEMPROXY_CLASS(HttpRequest);
     HttpRequest();
-
+    HttpRequest(method_t aMethod, protocol_t aProtocol, const char *aUrlpath);
+    ~HttpRequest();
     virtual void reset();
 
-    bool multipartRangeRequest() const;
+protected:
+    void initXX(method_t aMethod, protocol_t aProtocol, const char *aUrlpath);
+    void clean();
+    void init();
+
+public:
+    int link_count;            /* free when zero */
 
+public:
     method_t method;
     char login[MAX_LOGIN_SZ];
     char host[SQUIDHOSTNAMELEN + 1];
@@ -69,7 +74,6 @@ public:
     u_short port;
     String urlpath;
     char *canonical;
-    int link_count;            /* free when zero */
     request_flags flags;
     HttpHdrRange *range;
     time_t ims;
@@ -95,9 +99,14 @@ public:
     String extacl_log;         /* String to be used for access.log purposes */
 
 public:
+    bool multipartRangeRequest() const;
     bool parseFirstLine(const char *start, const char *end);
     int parseHeader(const char *parse_start);
     virtual bool expectingBody(method_t unused, ssize_t&) const;
+    int prefixLen();
+    void swapOut(StoreEntry * e);
+    void pack(Packer * p);
+    static void httpRequestPack(void *obj, Packer *p);
 
 private:
     const char *packableURI(bool full_uri) const;
@@ -107,8 +116,6 @@ protected:
     virtual bool sanityCheckStartLine(MemBuf *buf, http_status *error);
     virtual void hdrCacheInit();
 
-public: // should be private
-    void clean(); // low-level; treat as private
 };
 
 MEMPROXY_CLASS_INLINE(HttpRequest)
index fefac23dfbfdc3f33038ec970cc0bb9d5cda4cbf..02fac2e008816e24fa3ec4f909d3fac44800415a 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: client_side_reply.cc,v 1.92 2006/01/03 17:22:30 wessels Exp $
+ * $Id: client_side_reply.cc,v 1.93 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 88    Client-side Reply Routines
  * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
@@ -1059,9 +1059,9 @@ clientReplyContext::traceReply(clientStreamNode * node)
     rep = new HttpReply;
     HttpVersion version(1,0);
     rep->setHeaders(version, HTTP_OK, NULL, "text/plain",
-                    httpRequestPrefixLen(http->request), 0, squid_curtime);
+                    http->request->prefixLen(), 0, squid_curtime);
     rep->swapOut(http->storeEntry());
-    httpRequestSwapOut(http->request, http->storeEntry());
+    http->request->swapOut(http->storeEntry());
     http->storeEntry()->complete();
 }
 
@@ -2158,7 +2158,7 @@ clientReplyContext::createStoreEntry(method_t m, request_flags flags)
      */
 
     if (http->request == NULL)
-        http->request = requestLink(requestCreate(m, PROTO_NONE, null_string));
+        http->request = requestLink(new HttpRequest(m, PROTO_NONE, null_string));
 
     StoreEntry *e = storeCreateEntry(http->uri, http->log_uri, flags, m);
 
index b704d3dd1a1951608a1b7f3ffb02854770dff789..a56a045d085f6974081389d2f8abf5e730193a9e 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: htcp.cc,v 1.61 2006/01/04 17:19:41 wessels Exp $
+ * $Id: htcp.cc,v 1.62 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 31    Hypertext Caching Protocol
  * AUTHOR: Duane Wesssels
@@ -818,7 +818,7 @@ htcpSpecifier::checkHit()
 
     if (!httpHeaderParse(&checkHitRequest->header, req_hdrs, blk_end)) {
         debug(31, 3) ("htcpCheckHit: NO; failed to parse request headers\n");
-        requestDestroy(checkHitRequest);
+        delete checkHitRequest;
         checkHitRequest = NULL;
         checkedHit(NullStoreEntry::getInstance());
         return;
@@ -852,7 +852,7 @@ htcpSpecifier::created (StoreEntry *e)
     hit = e;
 
 miss:
-    requestDestroy(checkHitRequest);
+    delete checkHitRequest;
     checkedHit (hit);
 }
 
index 47e238d9d4d8ba7f7894a1ae2ffed5ed6d9e60a9..29df0f7f1ff7954d4758095fff7d4f564fb49559 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: http.cc,v 1.481 2006/01/09 20:42:35 wessels Exp $
+ * $Id: http.cc,v 1.482 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 11    Hypertext Transfer Protocol (HTTP)
  * AUTHOR: Harvest Derived
@@ -98,8 +98,8 @@ HttpStateData::HttpStateData(FwdState *theFwdState)
         else
             url = storeUrl(entry);
 
-        HttpRequest * proxy_req = requestCreate(orig_request->method,
-                                                orig_request->protocol, url);
+        HttpRequest * proxy_req = new HttpRequest(orig_request->method,
+                                  orig_request->protocol, url);
 
         xstrncpy(proxy_req->host, _peer->host, SQUIDHOSTNAMELEN);
 
index 29407dfe21c4734f6cc7ebcce65fb4425d270a6f..4bc43652c2dd6ff2037d34239fcd72305247e6de 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: icp_v2.cc,v 1.88 2005/12/08 20:08:47 wessels Exp $
+ * $Id: icp_v2.cc,v 1.89 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 12    Internet Cache Protocol
  * AUTHOR: Duane Wessels
@@ -98,7 +98,7 @@ ICPState::~ICPState()
     safe_free(url);
 
     if (request)
-        requestDestroy(request);
+        delete request;
 }
 
 
index 66112a0d20ff326d7702cd6b5f56e648186a998a..6e6e790ce195fefb91e124c9ffd4980f26dc2b3d 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: icp_v3.cc,v 1.39 2003/08/10 11:00:43 robertc Exp $
+ * $Id: icp_v3.cc,v 1.40 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 12    Internet Cache Protocol
  * AUTHOR: Duane Wessels
@@ -62,7 +62,7 @@ doV3Query(int fd, struct sockaddr_in from, char *buf, icp_common_t header)
     if (!icpAccessAllowed(&from, icp_request))
     {
         icpDenyAccess (&from, url, header.reqnum, fd);
-        requestDestroy(icp_request);
+        delete icp_request;
         return;
     }
 
index fd91b926e2e9fbf8ed4fcd0fccad4a72a33e1c6d..d5d5c73108f97fe36b283c1320f579076f11acd2 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: url.cc,v 1.148 2005/11/11 17:45:15 wessels Exp $
+ * $Id: url.cc,v 1.149 2006/01/19 18:40:28 wessels Exp $
  *
  * DEBUG: section 23    URL Parsing
  * AUTHOR: Duane Wessels
@@ -459,7 +459,7 @@ urlParse(method_t method, char *url, HttpRequest *request)
     }
 
     if (NULL == request)
-        request = requestCreate(method, protocol, urlpath);
+        request = new HttpRequest(method, protocol, urlpath);
     else {
         request->method = method;
         request->protocol = protocol;
@@ -476,7 +476,7 @@ static HttpRequest *
 urnParse(method_t method, char *urn)
 {
     debug(50, 5) ("urnParse: %s\n", urn);
-    return requestCreate(method, PROTO_URN, urn + 4);
+    return new HttpRequest(method, PROTO_URN, urn + 4);
 }
 
 const char *