abyss_session.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 **
00003 ** session.c
00004 **
00005 ** This file is part of the ABYSS Web server project.
00006 **
00007 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
00008 ** All rights reserved.
00009 **
00010 ** Redistribution and use in source and binary forms, with or without
00011 ** modification, are permitted provided that the following conditions
00012 ** are met:
00013 ** 1. Redistributions of source code must retain the above copyright
00014 **    notice, this list of conditions and the following disclaimer.
00015 ** 2. Redistributions in binary form must reproduce the above copyright
00016 **    notice, this list of conditions and the following disclaimer in the
00017 **    documentation and/or other materials provided with the distribution.
00018 ** 3. The name of the author may not be used to endorse or promote products
00019 **    derived from this software without specific prior written permission.
00020 ** 
00021 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00022 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00025 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031 ** SUCH DAMAGE.
00032 **
00033 *******************************************************************************/
00034 
00035 #include <assert.h>
00036 #include <sys/types.h>
00037 #include <string.h>
00038 #include <stdio.h>
00039 
00040 #include "abyss_xmlrpc_int.h"
00041 #include <xmlrpc-c/abyss.h>
00042 #include "abyss_server.h"
00043 #include "abyss_conn.h"
00044 
00045 #include "abyss_session.h"
00046 
00047 
00048 
00049 abyss_bool
00050 SessionRefillBuffer(TSession * const sessionP) {
00051 /*----------------------------------------------------------------------------
00052    Get the next chunk of data from the connection into the buffer.
00053 
00054    I.e. read data from the socket.
00055 -----------------------------------------------------------------------------*/
00056     struct _TServer * const srvP = sessionP->conn->server->srvP;
00057     abyss_bool succeeded;
00058             
00059     /* Reset our read buffer & flush data from previous reads. */
00060     ConnReadInit(sessionP->conn);
00061     
00062     /* Read more network data into our buffer.  If we encounter a
00063        timeout, exit immediately.  We're very forgiving about the
00064        timeout here.  We allow a full timeout per network read, which
00065        would allow somebody to keep a connection alive nearly
00066        indefinitely.  But it's hard to do anything intelligent here
00067        without very complicated code.
00068     */
00069     succeeded = ConnRead(sessionP->conn, srvP->timeout);
00070 
00071     return succeeded;
00072 }
00073 
00074 
00075 
00076 size_t
00077 SessionReadDataAvail(TSession * const sessionP) {
00078 
00079     return sessionP->conn->buffersize - sessionP->conn->bufferpos;
00080 
00081 }
00082 
00083 
00084 
00085 void
00086 SessionGetReadData(TSession *    const sessionP, 
00087                    size_t        const max, 
00088                    const char ** const outStartP, 
00089                    size_t *      const outLenP) {
00090 /*----------------------------------------------------------------------------
00091    Extract some data which the server has read and buffered for the
00092    session.  Don't get or wait for any data that has not yet arrived.
00093    Do not return more than 'max'.
00094 
00095    We return a pointer to the first byte as *outStartP, and the length in
00096    bytes as *outLenP.  The memory pointed to belongs to the session.
00097 -----------------------------------------------------------------------------*/
00098     uint32_t const bufferPos = sessionP->conn->bufferpos;
00099 
00100     *outStartP = &sessionP->conn->buffer[bufferPos];
00101 
00102     assert(bufferPos <= sessionP->conn->buffersize);
00103 
00104     *outLenP = MIN(max, sessionP->conn->buffersize - bufferPos);
00105 
00106     /* move pointer past the bytes we are returning */
00107     sessionP->conn->bufferpos += *outLenP;
00108 
00109     assert(sessionP->conn->bufferpos <= sessionP->conn->buffersize);
00110 }
00111 
00112 
00113 
00114 void
00115 SessionGetRequestInfo(TSession *            const sessionP,
00116                       const TRequestInfo ** const requestInfoPP) {
00117     
00118     *requestInfoPP = &sessionP->request_info;
00119 }
00120 
00121 
00122 
00123 abyss_bool
00124 SessionLog(TSession * const sessionP) {
00125 
00126     abyss_bool retval;
00127 
00128     if (!sessionP->validRequest)
00129         retval = FALSE;
00130     else {
00131         const char * const user = sessionP->request_info.user;
00132 
00133         const char * logline;
00134         char date[30];
00135 
00136         DateToLogString(&sessionP->date, date);
00137 
00138         xmlrpc_asprintf(&logline, "%d.%d.%d.%d - %s - [%s] \"%s\" %d %d",
00139                         IPB1(sessionP->conn->peerip),
00140                         IPB2(sessionP->conn->peerip),
00141                         IPB3(sessionP->conn->peerip),
00142                         IPB4(sessionP->conn->peerip),
00143                         user ? user : "",
00144                         date, 
00145                         sessionP->request_info.requestline,
00146                         sessionP->status,
00147                         sessionP->conn->outbytes
00148             );
00149         if (logline) {
00150             LogWrite(sessionP->conn->server, logline);
00151 
00152             xmlrpc_strfree(logline);
00153         }
00154         retval = TRUE;
00155     }
00156     return retval;
00157 }
00158 
00159 
00160 

Generated on Thu May 17 10:00:23 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6