From: wessels <> Date: Sat, 14 Jan 2006 07:06:19 +0000 (+0000) Subject: ConnStateData and ClientBody was a little confusing. There was some duplication X-Git-Tag: SQUID_3_0_PRE4~366 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83fdd41a80216cba0142fc633ee7e9a79434c165;p=thirdparty%2Fsquid.git ConnStateData and ClientBody was a little confusing. There was some duplication with ClientBody members also appearing in ConnStateData.body. They have been merged now and ClientBody lives in its own .{cc,h} files. --- diff --git a/src/ClientBody.cc b/src/ClientBody.cc new file mode 100644 index 0000000000..e31f541616 --- /dev/null +++ b/src/ClientBody.cc @@ -0,0 +1,109 @@ +#include "squid.h" +#include "client_side.h" +#include "ClientBody.h" +#include "HttpRequest.h" + + +ClientBody::ClientBody(ConnStateData::Pointer & aConn, HttpRequest *Request) : conn(aConn), request(NULL), buf (NULL), bufsize(0), callback(NULL), cbdata(NULL) +{ + request = requestLink(Request); +} + +ClientBody::~ClientBody() +{ + assert(callback == NULL); + assert(buf == NULL); + requestUnlink(request); +} + +/* Called by clientReadRequest to process body content */ +void +ClientBody::process() +{ + + debug(33, 2) ("clientBody::process: start FD %d body_size=%lu in.notYetUsed=%lu cb=%p req=%p\n", + conn->fd, + (unsigned long int) conn->body_size_left, + (unsigned long int) conn->in.notYetUsed, + callback, + request); + + if (conn->in.notYetUsed) + processBuffer(); + else + conn->readSomeData(); +} + +void +ClientBody::processBuffer() +{ + /* Some sanity checks... */ + assert(conn->body_size_left > 0); + assert(conn->in.notYetUsed > 0); + assert(callback != NULL); + assert(buf != NULL); + /* How much do we have to process? */ + size_t size = conn->in.notYetUsed; + + if (size > conn->body_size_left) /* only process the body part */ + size = conn->body_size_left; + + if (size > bufsize) /* don't copy more than requested */ + size = bufsize; + + xmemcpy(buf, conn->in.buf, size); + + conn->body_size_left -= size; + + /* Move any remaining data */ + conn->in.notYetUsed -= size; + + if (conn->in.notYetUsed > 0) + xmemmove(conn->in.buf, conn->in.buf + size, conn->in.notYetUsed); + + /* Remove request link if this is the last part of the body, as + * clientReadRequest automatically continues to process next request */ + if (conn->body_size_left <= 0 && request != NULL) + request->body_connection = NULL; + + request->flags.body_sent = 1; + + doCallback(size); + + debug(33, 2) ("ClientBody::process: end FD %d size=%lu body_size=%lu in.notYetUsed=%lu cb=%p req=%p\n", + conn->fd, (unsigned long int)size, (unsigned long int) conn->body_size_left, + (unsigned long) conn->in.notYetUsed, callback, request); +} + +void +ClientBody::init(char *Buf, size_t Bufsize, CBCB *Callback, void *Cbdata) +{ + buf = Buf; + bufsize = Bufsize; + callback = Callback; + cbdata = cbdataReference(Cbdata); +} + +void +ClientBody::doCallback(size_t theSize) +{ + char *theBuf = buf; + CBCB *theCallback = callback; + void *theCbdata = cbdata; + + buf = NULL; + bufsize = 0; + callback = NULL; + cbdata = NULL; + + void *someCbdata; + + if (cbdataReferenceValidDone(theCbdata, &someCbdata)) + theCallback(theBuf, theSize, someCbdata); +} + +void +ClientBody::negativeCallback() +{ + doCallback((size_t)-1); +} diff --git a/src/ClientBody.h b/src/ClientBody.h new file mode 100644 index 0000000000..2aaa450d0b --- /dev/null +++ b/src/ClientBody.h @@ -0,0 +1,29 @@ +#ifndef SQUID_CLIENTBODY_H +#define SQUID_CLIETNBODY_H + +class ClientBody +{ + +public: + ClientBody (ConnStateData::Pointer &, HttpRequest *); + ~ClientBody(); + void process(); + void processBuffer(); + void init(char *, size_t, CBCB *, void *); +bool hasCallback() const { return callback ? true : false; }; + + void doCallback(size_t); + void negativeCallback(); + HttpRequest * getRequest() { return request; }; + +private: + ConnStateData::Pointer conn; + HttpRequest *request; + char *buf; + size_t bufsize; + CBCB *callback; + void *cbdata; +}; + + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index 2c0dec0bf9..78453d7d7f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ # # Makefile for the Squid Object Cache server # -# $Id: Makefile.am,v 1.125 2006/01/09 18:02:12 serassio Exp $ +# $Id: Makefile.am,v 1.126 2006/01/14 00:06:19 wessels Exp $ # # Uncomment and customize the following to suit your needs: # @@ -369,6 +369,8 @@ squid_SOURCES = \ client_side_reply.h \ client_side_request.cc \ client_side_request.h \ + ClientBody.h \ + ClientBody.cc \ ClientRequestContext.h \ clientStream.cc \ clientStream.h \ diff --git a/src/client_side.cc b/src/client_side.cc index 5fbaea6844..7f6f4a1783 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -1,6 +1,6 @@ /* - * $Id: client_side.cc,v 1.707 2006/01/03 21:47:59 wessels Exp $ + * $Id: client_side.cc,v 1.708 2006/01/14 00:06:19 wessels Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -72,6 +72,7 @@ #include "client_side_reply.h" #include "ClientRequestContext.h" #include "MemBuf.h" +#include "ClientBody.h" #if LINGERING_CLOSE #define comm_close comm_lingering_close @@ -625,6 +626,9 @@ ConnStateData::~ConnStateData() auth_user_request = NULL; cbdataReferenceDone(port); + + if (body) + delete body; } /* @@ -1501,7 +1505,7 @@ ClientSocketContext::initiateClose() return; } - if (http->getConn()->body.size_left > 0) { + if (http->getConn()->body_size_left > 0) { debug(33, 5) ("ClientSocketContext::initiateClose: closing, but first we need to read the rest of the request\n"); /* XXX We assumes the reply does fit in the TCP transmit window. * If not the connection may stall while sending the reply @@ -2155,7 +2159,7 @@ clientAfterReadingRequests(int fd, ConnStateData::Pointer &conn, int do_next_rea /* Check if a half-closed connection was aborted in the middle */ if (F->flags.socket_eof) { - if (conn->in.notYetUsed != conn->body.size_left) { + if (conn->in.notYetUsed != conn->body_size_left) { /* != 0 when no request body */ /* Partial request received. Abort client connection! */ debug(33, 3) ("clientAfterReadingRequests: FD %d aborted, partial request\n", fd); @@ -2283,7 +2287,8 @@ clientProcessRequest(ConnStateData::Pointer &conn, ClientSocketContext *context, /* Do we expect a request-body? */ if (request->content_length > 0) { - conn->body.size_left = request->content_length; + conn->body = new ClientBody(conn, request); + conn->body_size_left = request->content_length; request->body_connection = conn; /* Is it too large? */ @@ -2355,7 +2360,7 @@ clientParseRequest(ConnStateData::Pointer conn, bool &do_next_read) debug(33, 5) ("clientParseRequest: FD %d: attempting to parse\n", conn->fd); - while (conn->in.notYetUsed > 0 && conn->body.size_left == 0) { + while (conn->in.notYetUsed > 0 && conn->body_size_left == 0) { size_t req_line_sz; connStripBufferWhitespace (conn); @@ -2404,9 +2409,9 @@ clientParseRequest(ConnStateData::Pointer conn, bool &do_next_read) break; } - continue; /* while offset > 0 && body.size_left == 0 */ + continue; /* while offset > 0 && body_size_left == 0 */ } - } /* while offset > 0 && conn->body.size_left == 0 */ + } /* while offset > 0 && conn->body_size_left == 0 */ return parsed_req; } @@ -2477,9 +2482,8 @@ clientReadRequest(int fd, char *buf, size_t size, comm_err_t flag, int xerrno, /* Process request body if any */ - if (conn->in.notYetUsed > 0 && conn->body.callback != NULL) { - ClientBody body(conn); - body.process(); + if (conn->in.notYetUsed > 0 && conn->body && conn->body->hasCallback()) { + conn->body->process(); } /* Process next request */ @@ -2518,104 +2522,10 @@ clientReadBody(HttpRequest * request, char *buf, size_t size, CBCB * callback, } debug(33, 2) ("clientReadBody: start FD %d body_size=%lu in.notYetUsed=%ld cb=%p req=%p\n", - conn->fd, (unsigned long int) conn->body.size_left, + conn->fd, (unsigned long int) conn->body_size_left, (unsigned long int) conn->in.notYetUsed, callback, request); - conn->body.callback = callback; - conn->body.cbdata = cbdataReference(cbdata); - conn->body.buf = buf; - conn->body.bufsize = size; - conn->body.request = requestLink(request); - ClientBody body (conn); - body.process(); -} - -ClientBody::ClientBody(ConnStateData::Pointer &aConn) : conn(aConn), buf (NULL), callback(NULL), request(NULL) -{} - -void -ClientBody::preProcessing() -{ - callback = conn->body.callback; - request = conn->body.request; - /* Note: request is null while eating "aborted" transfers */ - debug(33, 2) ("clientBody::process: start FD %d body_size=%lu in.notYetUsed=%lu cb=%p req=%p\n", - conn->fd, (unsigned long int) conn->body.size_left, - (unsigned long int) conn->in.notYetUsed, callback, request); -} - -/* Called by clientReadRequest to process body content */ -void -ClientBody::process() -{ - preProcessing(); - - if (conn->in.notYetUsed) - processBuffer(); - else - conn->readSomeData(); -} - -void -ClientBody::processBuffer() -{ - /* Some sanity checks... */ - assert(conn->body.size_left > 0); - assert(conn->in.notYetUsed > 0); - assert(callback != NULL); - buf = conn->body.buf; - assert(buf != NULL); - /* How much do we have to process? */ - size_t size = conn->in.notYetUsed; - - if (size > conn->body.size_left) /* only process the body part */ - size = conn->body.size_left; - - if (size > conn->body.bufsize) /* don't copy more than requested */ - size = conn->body.bufsize; - - xmemcpy(buf, conn->in.buf, size); - - conn->body.size_left -= size; - - /* Move any remaining data */ - conn->in.notYetUsed -= size; - - if (conn->in.notYetUsed > 0) - xmemmove(conn->in.buf, conn->in.buf + size, conn->in.notYetUsed); - - /* Remove request link if this is the last part of the body, as - * clientReadRequest automatically continues to process next request */ - if (conn->body.size_left <= 0 && request != NULL) - request->body_connection = NULL; - - /* Remove clientReadBody arguments (the call is completed) */ - conn->body.request = NULL; - - conn->body.callback = NULL; - - conn->body.buf = NULL; - - conn->body.bufsize = 0; - - /* Remember that we have touched the body, not restartable */ - if (request != NULL) { - request->flags.body_sent = 1; - conn->body.request = NULL; - } - - /* Invoke callback function */ - void *cbdata; - - if (cbdataReferenceValidDone(conn->body.cbdata, &cbdata)) - callback(buf, size, cbdata); - - if (request != NULL) { - requestUnlink(request); /* Linked in clientReadBody */ - } - - debug(33, 2) ("ClientBody::process: end FD %d size=%lu body_size=%lu in.notYetUsed=%lu cb=%p req=%p\n", - conn->fd, (unsigned long int)size, (unsigned long int) conn->body.size_left, - (unsigned long) conn->in.notYetUsed, callback, request); + conn->body->init(buf, size, callback, cbdata); + conn->body->process(); } /* A dummy handler that throws away a request-body */ @@ -2625,16 +2535,13 @@ clientReadBodyAbortHandler(char *buf, ssize_t size, void *data) static char bodyAbortBuf[SQUID_TCP_SO_RCVBUF]; ConnStateData *conn = (ConnStateData *) data; debug(33, 2) ("clientReadBodyAbortHandler: FD %d body_size=%lu in.notYetUsed=%lu\n", - conn->fd, (unsigned long int) conn->body.size_left, + conn->fd, (unsigned long int) conn->body_size_left, (unsigned long) conn->in.notYetUsed); - if (size != 0 && conn->body.size_left != 0) { + if (size != 0 && conn->body_size_left != 0) { debug(33, 3) ("clientReadBodyAbortHandler: FD %d shedule next read\n", conn->fd); - conn->body.callback = clientReadBodyAbortHandler; - conn->body.buf = bodyAbortBuf; - conn->body.bufsize = sizeof(bodyAbortBuf); - conn->body.cbdata = cbdataReference(data); + conn->body->init(bodyAbortBuf, sizeof(bodyAbortBuf), clientReadBodyAbortHandler, data); } if (conn->closing()) @@ -2646,30 +2553,15 @@ int clientAbortBody(HttpRequest * request) { ConnStateData::Pointer conn = request->body_connection; - char *buf; - CBCB *callback; - if (conn.getRaw() == NULL || conn->body.size_left <= 0 || conn->body.request != request) + if (conn.getRaw() == NULL || conn->body_size_left <= 0 || conn->body->getRequest() != request) return 0; /* No body to abort */ - if (conn->body.callback != NULL) { - buf = conn->body.buf; - callback = conn->body.callback; - assert(request == conn->body.request); - conn->body.buf = NULL; - conn->body.callback = NULL; - conn->body.request = NULL; - void *cbdata; - - if (cbdataReferenceValidDone(conn->body.cbdata, &cbdata)) - callback(buf, -1, cbdata); /* Signal abort to clientReadBody caller */ - - conn->body.cbdata = NULL; - - requestUnlink(request); - } + if (conn->body->hasCallback()) + conn->body->negativeCallback(); clientReadBodyAbortHandler(NULL, -1, conn.getRaw()); /* Install abort handler */ + /* ClientBody::process() */ return 1; /* Aborted */ } @@ -3283,7 +3175,7 @@ ConnStateData::operator delete (void *address) cbdataFree(t); } -ConnStateData::ConnStateData() : transparent_ (false), reading_ (false), closing_ (false) +ConnStateData::ConnStateData() : body_size_left(0), transparent_ (false), reading_ (false), closing_ (false) { openReference = this; } diff --git a/src/client_side.h b/src/client_side.h index 418c49ef7e..e470e0a74f 100644 --- a/src/client_side.h +++ b/src/client_side.h @@ -1,6 +1,6 @@ /* - * $Id: client_side.h,v 1.12 2005/11/21 23:26:45 wessels Exp $ + * $Id: client_side.h,v 1.13 2006/01/14 00:06:19 wessels Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -43,6 +43,9 @@ class ClientHttpRequest; class clientStreamNode; +class ClientBody; +; + template class Range; @@ -160,17 +163,9 @@ public: in; - struct - { - size_t size_left; /* How much body left to process */ - HttpRequest *request; /* Parameters passed to clientReadBody */ - char *buf; - size_t bufsize; - CBCB *callback; - void *cbdata; - } + ClientBody *body; + size_t body_size_left; - body; auth_type_t auth_type; /* Is this connection based authentication? if so what type it is. */ /* note this is ONLY connection based because NTLM is against HTTP spec */ /* the user details for connection based authentication */ @@ -214,22 +209,6 @@ private: /* convenience class while splitting up body handling */ /* temporary existence only - on stack use expected */ -class ClientBody -{ - -public: - ClientBody (ConnStateData::Pointer &); - void process(); - void preProcessing(); - void processBuffer(); - -private: - ConnStateData::Pointer conn; - char *buf; - CBCB *callback; - HttpRequest *request; -}; - void setLogUri(ClientHttpRequest * http, char const *uri); const char *findTrailingHTTPVersion(const char *uriAndHTTPVersion, const char *end = NULL);