SBinetHttpStream.hpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #ifndef SBINETHTTPSTREAM_HPP
  23. #define SBINETHTTPSTREAM_HPP
  24. #include "SBinetStoppableStream.hpp"
  25. class SBinetChannel;
  26. class SBinetHttpConnection;
  27. class SWIinputStream;
  28. class SWIoutputStream;
  29. class SBinetValidator;
  30. /**
  31.  * This class is responsible to fetch HTTP documents.
  32.  **/
  33. class SBinetHttpStream : public SBinetStoppableStream
  34. {
  35.  public:
  36.   enum SubmitMethod { GET_METHOD = 1, POST_METHOD = 2 };
  37.   enum EncodingType { TYPE_URL_ENCODED = 1, TYPE_MULTIPART = 2 };
  38.  public:
  39.   SBinetHttpStream(SBinetURL* url, SubmitMethod method,
  40.                    SBinetChannel* ch, SBinetValidator* cacheValidator,
  41.                    VXIlogInterface *log, VXIunsigned diagLogBase);
  42.   ~SBinetHttpStream();
  43.   VXIinetResult Open(VXIint flags,
  44.      const VXIMap* properties,
  45.      VXIMap* streamInfo);
  46.   VXIinetResult Read(/* [OUT] */ VXIbyte*         pBuffer,
  47.                      /* [IN]  */ VXIulong         nBuflen,
  48.                      /* [OUT] */ VXIulong*        pnRead );
  49.   VXIinetResult Write(/* [IN]  */ const VXIbyte*   pBuffer,
  50.                       /* [IN]  */ VXIulong         nBuflen,
  51.                       /* [OUT] */ VXIulong*        pnWritten);
  52.   VXIinetResult Close();
  53.   SBinetChannel* getChannel()
  54.   {
  55.     return _channel;
  56.   }
  57.  public:
  58.   int getHttpStatus() const
  59.   {
  60.     return _HttpStatus;
  61.   }
  62.  public:
  63.   const SBinetValidator *getValidator() const
  64.   {
  65.     return _validator;
  66.   }
  67.  private:
  68.   struct HeaderInfo;
  69.   typedef void (*HeaderHandler)(HeaderInfo *,
  70.                                 const char *value,
  71.                                 SBinetHttpStream *,
  72.                                 VXIMap *);
  73.   struct HeaderInfo
  74.   {
  75.     const char *header;
  76.     const VXIchar *inet_property;
  77.     int value_type;
  78.     HeaderHandler handler;
  79.   };
  80.   VXIinetResult getStatus();
  81.   VXIinetResult parseHeaders(VXIMap *streamInfo);
  82.   void processHeader(const char *header,
  83.                      const char *value,
  84.                      VXIMap *streamInfo);
  85.   static void connectionHandler(HeaderInfo *headerInfo,
  86.                                 const char *value,
  87.                                 SBinetHttpStream *stream,
  88.                                 VXIMap *streamInfo);
  89.   static void setCookieHandler(HeaderInfo *headerInfo,
  90.                                const char *value,
  91.                                SBinetHttpStream *stream,
  92.                                VXIMap *streamInfo);
  93.   static void setCookie2Handler(HeaderInfo *headerInfo,
  94.                                 const char *value,
  95.                                 SBinetHttpStream *stream,
  96.                                 VXIMap *streamInfo);
  97.   static void defaultHeaderHandler(HeaderInfo *headerInfo,
  98.                                    const char *value,
  99.                                    SBinetHttpStream *stream,
  100.                                    VXIMap *streamInfo);
  101.   static void transferEncodingHandler(HeaderInfo *headerInfo,
  102.                                       const char *value,
  103.                                       SBinetHttpStream *stream,
  104.                                       VXIMap *streamInfo);
  105.   static void contentLengthHandler(HeaderInfo *headerInfo,
  106.                                    const char *value,
  107.                                    SBinetHttpStream *stream,
  108.                                    VXIMap *streamInfo);
  109.   VXIinetResult doGet(VXIint32 flags,
  110.                       const VXIMap *properties,
  111.                       VXIMap *streamInfo);
  112.   VXIinetResult doPost(VXIint32 flags,
  113.                        const VXIMap *properties,
  114.                        VXIMap *streamInfo);
  115.   VXIinetResult flushAndCheckConnection();
  116.   VXIinetResult getHeaderInfo(VXIMap* streamInfo);
  117.   void processHeaderInfo(VXIMap *streamInfo);
  118.   void setValidatorInfo(VXIMap *streamInfo, time_t requestTime);
  119.   void setValidatorInfo(VXIMap *streamInfo, time_t requestTime, const VXIulong sizeBytes);
  120.   bool handleRedirect(VXIMap *streamInfo);
  121.   void sendCookies();
  122.   VXIinetResult MapError(int ht_error, const VXIchar **errorDesc);
  123.   VXIinetResult getChunkSize(VXIulong& chunkSize);
  124.   int readChunked(/* [OUT] */ VXIbyte*         buffer,
  125.                   /* [IN]  */ VXIulong         buflen);
  126.   int readNormal(/* [OUT] */ VXIbyte*         buffer,
  127.                  /* [IN]  */ VXIulong         buflen);
  128.  private:
  129.   VXIinetResult initSocket(SubmitMethod method,
  130.                            const VXIMap *properties,
  131.                            VXIMap *streamInfo);
  132.   void writeDebugTimeStamp();
  133.   int getValue(char *&value, unsigned long mask);
  134.   int getHeader(char *&header, char delimiter);
  135.   int getHeaderValue(char *&header, char *&value);
  136.   int _HttpStatus;
  137.   // The number of bytes to read in the current chunk if _chunked is true or
  138.   // in the stream if _chunked is falsed.
  139.   VXIulong _leftToRead;
  140.   bool _chunked;
  141.   SubmitMethod _method;
  142.   SBinetChannel* _channel;
  143.   SBinetHttpConnection *_connection;
  144.   SWIinputStream *_inputStream;
  145.   SWIoutputStream *_outputStream;
  146.   VXIint32 _closeConnection;
  147.   VXIint32 _connectionAborted;
  148.   static HeaderInfo headerInfoMap[];
  149.   static HeaderInfo* findHeaderInfo(const char *header);
  150.   VXIinetResult waitStreamReady();
  151.   EncodingType getEncodingType(const VXIMap *properties);
  152.   VXIinetResult skipBody();
  153.   SBinetValidator* _validator;
  154.   SBinetValidator* _cacheValidator;
  155. };
  156. #endif