NCBI C Toolkit Cross Reference

C/connect/ncbi_http_connector.h


  1 #ifndef CONNECT___HTTP_CONNECTOR__H
  2 #define CONNECT___HTTP_CONNECTOR__H
  3 
  4 /* $Id: ncbi_http_connector.h,v 6.19 2009/05/04 15:44:29 kazimird Exp $
  5  * ===========================================================================
  6  *
  7  *                            PUBLIC DOMAIN NOTICE
  8  *               National Center for Biotechnology Information
  9  *
 10  *  This software/database is a "United States Government Work" under the
 11  *  terms of the United States Copyright Act.  It was written as part of
 12  *  the author's official duties as a United States Government employee and
 13  *  thus cannot be copyrighted.  This software/database is freely available
 14  *  to the public for use. The National Library of Medicine and the U.S.
 15  *  Government have not placed any restriction on its use or reproduction.
 16  *
 17  *  Although all reasonable efforts have been taken to ensure the accuracy
 18  *  and reliability of the software and data, the NLM and the U.S.
 19  *  Government do not and cannot warrant the performance or results that
 20  *  may be obtained by using this software or data. The NLM and the U.S.
 21  *  Government disclaim all warranties, express or implied, including
 22  *  warranties of performance, merchantability or fitness for any particular
 23  *  purpose.
 24  *
 25  *  Please cite the author in any work or product based on this material.
 26  *
 27  * ===========================================================================
 28  *
 29  * Author:  Denis Vakatov
 30  *
 31  * File Description:
 32  *   Implement CONNECTOR for the HTTP-based network connection
 33  *
 34  *   See in "ncbi_connector.h" for the detailed specification of the underlying
 35  *   connector("CONNECTOR", "SConnectorTag") methods and structures.
 36  *
 37  */
 38 
 39 #include <connect/ncbi_connutil.h>
 40 
 41 
 42 /** @addtogroup Connectors
 43  *
 44  * @{
 45  */
 46 
 47 
 48 #ifdef __cplusplus
 49 extern "C" {
 50 #endif
 51 
 52 
 53 /* Create new CONNECTOR structure to hit the specified URL using HTTP
 54  * with either POST or GET method.
 55  * Use the configuration values recorded in "net_info". If "net_info" is NULL,
 56  * then use the default info (created by "ConnNetInfo_Create(0)").
 57  *
 58  * In order to workaround some HTTP communication features, this code does:
 59  *  1) Accumulate all output data in an internal memory buffer until the
 60  *     first "Read" (or "Peek", or "Close", or "Wait" on read) is attempted
 61  *     (also see fHCC_Flushable flag below).
 62  *  2) On the first "Read" (or "Peek", or "Close", or "Wait" on read), compose
 63  *     and send the whole HTTP request as:
 64  *        {POST|GET} <info->path>?<info->args> HTTP/1.0\r\n
 65  *        <user_header\r\n>
 66  *        Content-Length: <accumulated_data_length>\r\n
 67  *        \r\n
 68  *        <accumulated_data>
 69  *     NOTE:
 70  *       if <user->header> is neither a NULL pointer nor an empty string, then:
 71  *       - it must NOT contain "empty lines":  '\r\n\r\n';
 72  *       - it must be terminated by a single '\r\n';
 73  *       - it gets inserted to the HTTP header "as is", without any
 74  *         automatic checking or encoding.
 75  *     NOTE:
 76  *       Data may depart to server side earlier if Flush()'ed in
 77  *       fHCC_Flushable connector, see below in "flags".
 78  *     After the request has been sent, reply data from the peer
 79  *     CGI program are then can be actually read out.
 80  *  4) On any "Write" operation. which follows data reading, the connection
 81  *     to the peer CGI program is forcedly closed (the peer CGI process will
 82  *     presumably die if has not done yet so), and data to be written are
 83  *     again are stored in the buffer until next "Read" etc, see item 1).
 84  *
 85  *  *) If "fHCC_AutoReconnect" is set in "flags", then the connector makes
 86  *     an automatic reconnect to the same CGI program with just the
 87  *     same parameters, and micro-session steps (1,2,3) are repeated with
 88  *     another instance of the CGI program.
 89  *
 90  *     If "fHCC_AutoReconnect" is not set then only one
 91  *     "Write ... Write Read ... Read" micro-session is allowed, any
 92  *     following "Write" attempt fails with error status "eIO_Closed".
 93  *
 94  *  Other flags:
 95  *
 96  *  fHCC_SureFlush --
 97  *       make the connector to send at least the HTTP header on "CLOSE" and
 98  *       re-"CONNECT", even if no data was written
 99  *  fHCC_KeepHeader --
100  *       do not strip HTTP header (i.e. everything up to the first "\r\n\r\n",
101  *       including the "\r\n\r\n") from the CGI script's response
102  *  fHCC_UrlDecodeInput --
103  *       strip the HTTP header from the input data;  assume the input
104  *       data are single-part, URL-encoded;  perform the URL-decoding on read
105  *       NOTE:  this flag disables the "fHCC_KeepHeader" flag
106  *  fHCC_DropUnread --
107  *       do not collect incoming data in "Read" mode before switching into
108  *       "Write" mode for storing output data in buffer; by default all
109  *       data sent by the CGI program are stored even if not all requested
110  *       before "Write" following "Read" was issued (stream emulation)
111  *  fHCC_NoUpread --
112  *       do not do internal reading into temporary buffer while sending
113  *       data to CGI program; by default any send operation tries to
114  *       extract data(if any) coming back from the CGI program in order to
115  *       prevent connection blocking
116  *  fHCC_Flushable --
117  *       usually all data written to the connection are kept until
118  *       read begins (even though Flush() might have been called in between
119  *       the writes).  With this flag set, Flush() will result the data
120  *       to be actually sent to server side, so the following write will form
121  *       new request, and not get added to the previous one.
122  *  fHCC_InsecureRedirect --
123  *       for security reasons the following redirects comprise security risk
124  *       and, thus, are prohibited:  switching from https to http, and
125  *       re-posting data (regardless of the transport, either http or https).
126  *       This flag allows such redirects (if needed) to be honored.
127  *  fHCC_NoAutoRetry --
128  *       do not attempt any auto-retries in case of failing connections
129  *       (this flag effectively means having SConnNetInfo::max_try set to 1).
130  *
131  * NOTE: the URL encoding/decoding (in the "fHCC_Url_*" cases and "info->args")
132  *       is performed by URL_Encode() and URL_Decode() -- "ncbi_connutil.[ch]".
133  */
134 
135 typedef enum {
136     fHCC_AutoReconnect    = 0x1,  /* see (*) above                           */
137     fHCC_SureFlush        = 0x2,  /* always send HTTP request on CLOSE/RECONN*/
138     fHCC_KeepHeader       = 0x4,  /* dont strip HTTP header from CGI response*/
139     fHCC_UrlDecodeInput   = 0x8,  /* strip HTTP header, URL-decode content   */
140     fHCC_UrlEncodeOutput  = 0x10, /* URL-encode all output data              */
141     fHCC_UrlCodec         = 0x18, /* fHCC_UrlDecodeInput | ...EncodeOutput   */
142     fHCC_UrlEncodeArgs    = 0x20, /* URL-encode "info->args"                 */
143     fHCC_DropUnread       = 0x40, /* each microsession drops yet unread data */
144     fHCC_NoUpread         = 0x80, /* do not use SOCK_SetReadOnWrite() at all */
145     fHCC_Flushable        = 0x100,/* connector will really flush on Flush()  */
146     fHCC_InsecureRedirect = 0x200,/* any redirect will be honored            */
147     fHCC_NoAutoRetry      = 0x400 /* no auto-retries allowed                 */
148 } EHCC_Flags;
149 typedef unsigned int THCC_Flags;  /* bitwise OR of "EHCC_Flags"              */
150 
151 extern NCBI_XCONNECT_EXPORT CONNECTOR HTTP_CreateConnector
152 (const SConnNetInfo* net_info,
153  const char*         user_header,
154  THCC_Flags          flags
155  );
156 
157 
158 /* An extended version of HTTP_CreateConnector() to change the URL of the
159  * server CGI "on-the-fly":
160  *  -- "parse_http_hdr()" is called each time the HTTP header received
161  *      from HTTP server and only if fHCC_KeepHeader is NOT set; false return
162  *      value is equivalent of having an error from the HTTP server itself.
163  *  -- "adjust_net_info()" is invoked each time before starting a
164  *      new "HTTP micro-session" making a hit if the prior hit has failed;
165  *      it is passed "net_info" stored in the connector, and the number of
166  *      previously unsuccessful attempts since the connection was opened;
167  *      false return value terminates retry attempts.
168  *  -- "adjust_cleanup()" is called when the connector is being destroyed.
169  */
170 
171 typedef int/*bool*/ (*FHttpParseHTTPHeader)
172 (const char* http_header,           /* HTTP header to parse, '\0'-terminated */
173  void*       adjust_data,           /* supplemental user data                */
174  int         server_error           /* != 0 if HTTP error                    */
175  );
176 
177 typedef int/*bool*/ (*FHttpAdjustNetInfo)
178 (SConnNetInfo* net_info,            /* net_info to adjust (in place)         */
179  void*         adjust_data,         /* supplemental user data                */
180  unsigned int  failure_count        /* how many failures since open          */
181  );
182 
183 typedef void (*FHttpAdjustCleanup)
184 (void* adjust_data                  /* supplemental user data for cleanup    */
185  );
186 
187 extern NCBI_XCONNECT_EXPORT CONNECTOR HTTP_CreateConnectorEx
188 (const SConnNetInfo*  net_info,
189  THCC_Flags           flags,
190  FHttpParseHTTPHeader parse_http_hdr, /* may be NULL, then no addtl. parsing */
191  FHttpAdjustNetInfo   adjust_net_info,/* may be NULL, then no adjustments    */
192  void*                adjust_data,    /* for "adjust_info" & "adjust_cleanup"*/
193  FHttpAdjustCleanup   adjust_cleanup  /* may be NULL                         */
194 );
195 
196 
197 /* Set message hook procedure for messages originating from NCBI via HTTP.
198  * Any hook will be called not more than once.  Until no hook is installed,
199  * and exactly one message is caught, a warning will be generated in
200  * the standard log file upon each message acceptance.
201  */
202 
203 typedef void (*FHTTP_NcbiMessageHook)(const char* message);
204 
205 extern NCBI_XCONNECT_EXPORT void HTTP_SetNcbiMessageHook
206 (FHTTP_NcbiMessageHook            /* New hook to be installed, NULL to reset */
207  );
208 
209 
210 #ifdef __cplusplus
211 }  /* extern "C" */
212 #endif
213 
214 
215 /* @} */
216 
217 #endif /* CONNECT___HTTP_CONNECTOR__H */
218 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.