⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apachehttprequest.cpp

📁 "More for C++" is a class library that provides some features that are usually common for object ori
💻 CPP
字号:
////  This file is part of the "More for C++" library////  Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)////  The "More for C++" library is free software; you can redistribute it and/or//  modify it under the terms of the license that comes with this package.////  Read "license.txt" for more details.////  THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED//  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES//  OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include <apache.hpp>#include <more/create.hpp>#include <more/util/vector.hpp>#include "apachehttprequest.hpp"using namespace more;using namespace more::servlet;using namespace more::servlet::apache;typedef more::util::Vector<String> StringVector;////////////////////////////////////////////////////////////////////////////////ApacheHttpRequest::ApacheHttpRequest(  request_rec* pRequest): m_pRequest( pRequest ){  if( getMethod( ) == HttpRequest::HTTP_POST )  {    parsePostedValues( );  }}////////////////////////////////////////////////////////////////////////////////String ApacheHttpRequest::getHost( ){  return ap_get_server_name( m_pRequest );}////////////////////////////////////////////////////////////////////////////////size_t ApacheHttpRequest::getPort( ){  return ap_get_server_port( m_pRequest );}////////////////////////////////////////////////////////////////////////////////String ApacheHttpRequest::getClient( ){  return ap_get_remote_host( m_pRequest -> connection,                             m_pRequest -> per_dir_config,                             REMOTE_NAME );}////////////////////////////////////////////////////////////////////////////////String ApacheHttpRequest::getFileName( ){  return m_pRequest -> filename;}////////////////////////////////////////////////////////////////////////////////String ApacheHttpRequest::getArguments( ){  return m_pRequest -> args;}////////////////////////////////////////////////////////////////////////////////HttpRequest::Method ApacheHttpRequest::getMethod( ){  return ( HttpRequest::Method ) m_pRequest -> method_number;}////////////////////////////////////////////////////////////////////////////////inline String stripWhiteSpace( const String& sString ){  size_t  nPosOfFirstChar = 0;  size_t  nPosOfLastChar;  while( nPosOfFirstChar < sString.getLength( ) && sString[nPosOfFirstChar] < 0x21 )  {    nPosOfFirstChar++;  }  nPosOfLastChar = nPosOfFirstChar;  while( nPosOfLastChar < sString.getLength( ) && sString[nPosOfLastChar] > 0x20 )  {    nPosOfLastChar++;  }  return sString.getSubstring( nPosOfFirstChar, nPosOfLastChar - nPosOfFirstChar );}////////////////////////////////////////////////////////////////////////////////String ApacheHttpRequest::getCookie(  const String& sName){  String        sResult;  Array<String> sCookies = getValues( "Cookie" );  for( size_t i = 0; i < sCookies.getLength( ) && sResult.getLength( ) == 0; i++ )  {    Array<String> sCookie = sCookies[i].split( ';' );    for( size_t j = 0; j < sCookie.getLength( ) && sResult.getLength( ) == 0; j++ )    {      Array<String> sKeyValuePair = sCookie[j].split( '=' );      if( sKeyValuePair.getLength( ) == 2 )      {        String sKey = stripWhiteSpace( sKeyValuePair[0] );        if( sKey == sName )        {          sResult = stripWhiteSpace( sKeyValuePair[1] );        }      }    }  }  return sResult;}////////////////////////////////////////////////////////////////////////////////static int ap_table_do_callback(  void*       pStringVector_void,  const char* pcKey,  const char* pcValue){  p<StringVector> pStringVector = ( StringVector* ) pStringVector_void;  pStringVector -> add( pcValue );  return 1;}////////////////////////////////////////////////////////////////////////////////Array<String> ApacheHttpRequest::getValues(  const String& sKey){  p<StringVector> pStringVector = CREATE StringVector;  ap_table_do( ap_table_do_callback, pStringVector, m_pRequest -> headers_in, ( const char* ) sKey, 0 );  return pStringVector -> toArray( );}////////////////////////////////////////////////////////////////////////////////void ApacheHttpRequest::redirect(  const String& sUrl){  m_pRequest -> status = 302;  ap_table_add( m_pRequest -> headers_out, "Location", sUrl );}////////////////////////////////////////////////////////////////////////////////void ApacheHttpRequest::parsePostedValues( ){  const char* pcContentType = ap_table_get( m_pRequest -> headers_in, "Content-Type" );  if( strcasecmp( pcContentType, "application/x-www-form-urlencoded" ) == 0 )  {    String        sRestOfHeader( readRestOfHeader( ) );    Array<String> keyValuePairs = sRestOfHeader.split( '&' );    for( size_t i = 0; i < keyValuePairs.getLength( ); i++ )    {      Array<String>  keyValuePair = keyValuePairs[i].split( '=' );      const char*    pcKey = "";      const char*    pcValue = "";      if( keyValuePair.getLength( ) > 0 )      {        pcKey = keyValuePair[0];        ap_unescape_url( ( char* ) pcKey );        if( keyValuePair.getLength( ) > 1 )        {          pcValue = keyValuePair[1];          ap_unescape_url( ( char* ) pcValue );        }        ap_table_merge( m_pRequest -> headers_in, pcKey, pcValue );      }    }  }}////////////////////////////////////////////////////////////////////////////////String ApacheHttpRequest::readRestOfHeader( ){  char* pcResult = "";  if( ap_setup_client_block( m_pRequest, REQUEST_CHUNKED_ERROR ) == OK )  {    if( ap_should_client_block( m_pRequest ) )    {      size_t  nTotalRead = 0;      size_t  nExpected = m_pRequest -> remaining;      pcResult = ( char* ) ap_pcalloc( m_pRequest -> pool, nExpected + 1 );      ap_hard_timeout( "ApacheHttpRequest::readPostedValues", m_pRequest );      while( nTotalRead < nExpected )      {        char  buffer[8192];        size_t  nRead = ap_get_client_block( m_pRequest, buffer, sizeof( buffer ) );        ap_reset_timeout( m_pRequest );        if( nTotalRead + nRead > nExpected )        {          nRead = nExpected - nTotalRead;        }        memcpy( pcResult + nTotalRead, buffer, nRead );        nTotalRead += nRead;      }      ap_kill_timeout( m_pRequest );      pcResult[nTotalRead] = 0x00;    }  }  return pcResult;}////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -