📄 ncbi_http_connector.c
字号:
/* store the write timeout */ if (timeout) { uuu->ww_timeout = *timeout; uuu->w_timeout = &uuu->ww_timeout; } else uuu->w_timeout = timeout; return eIO_Success;}static EIO_Status s_VT_Flush(CONNECTOR connector, const STimeout* timeout){ SHttpConnector* uuu = (SHttpConnector*) connector->handle; /* The real flush will be performed on the first "READ" (or "CLOSE"), * or on "WAIT". Here, we just store the write timeout, that's all... */ if (timeout) { uuu->ww_timeout = *timeout; uuu->w_timeout = &uuu->ww_timeout; } else uuu->w_timeout = timeout; return eIO_Success;}static EIO_Status s_VT_Read(CONNECTOR connector, void* buf, size_t size, size_t* n_read, const STimeout* timeout){ SHttpConnector* uuu = (SHttpConnector*) connector->handle; EIO_Status status = s_PreRead(uuu, timeout, 0/*no drop unread*/); size_t x_read = BUF_Read(uuu->r_buf, buf, size); *n_read = x_read; if (x_read < size) { if (status != eIO_Success) return status; status = s_Read(uuu, (char*) buf + x_read, size - x_read, n_read); *n_read += x_read; } else status = eIO_Success; return *n_read ? eIO_Success : status;}static EIO_Status s_VT_Status(CONNECTOR connector, EIO_Event dir){ SHttpConnector* uuu = (SHttpConnector*) connector->handle; return uuu->sock ? SOCK_Status(uuu->sock, dir) : (uuu->can_connect == eCC_None ? eIO_Closed : eIO_Success);}static EIO_Status s_VT_Close(CONNECTOR connector, const STimeout* timeout){ s_FlushAndDisconnect((SHttpConnector*) connector->handle, timeout, 1/*close*/); return eIO_Success;}#ifdef IMPLEMENTED__CONN_WaitAsyncstatic EIO_Status s_VT_WaitAsync(void* connector, FConnectorAsyncHandler func, SConnectorAsyncHandler* data){ return eIO_NotSupported;}#endifstatic void s_Setup(SMetaConnector *meta, CONNECTOR connector){ SHttpConnector* uuu = (SHttpConnector*) connector->handle; /* initialize virtual table */ CONN_SET_METHOD(meta, get_type, s_VT_GetType, connector); CONN_SET_METHOD(meta, descr, s_VT_Descr, connector); CONN_SET_METHOD(meta, open, s_VT_Open, connector); CONN_SET_METHOD(meta, wait, s_VT_Wait, connector); CONN_SET_METHOD(meta, write, s_VT_Write, connector); CONN_SET_METHOD(meta, flush, s_VT_Flush, connector); CONN_SET_METHOD(meta, read, s_VT_Read, connector); CONN_SET_METHOD(meta, status, s_VT_Status, connector); CONN_SET_METHOD(meta, close, s_VT_Close, connector);#ifdef IMPLEMENTED__CONN_WaitAsync CONN_SET_METHOD(meta, wait_async, s_VT_WaitAsync, connector);#endif CONN_SET_DEFAULT_TIMEOUT(meta, uuu->net_info->timeout);}static void s_Destroy(CONNECTOR connector){ SHttpConnector* uuu = (SHttpConnector*) connector->handle; if (uuu->adjust_cleanup) uuu->adjust_cleanup(uuu->adjust_data); ConnNetInfo_Destroy(uuu->net_info); BUF_Destroy(uuu->http); BUF_Destroy(uuu->r_buf); BUF_Destroy(uuu->w_buf); free(uuu); connector->handle = 0; free(connector);}/*********************************************************************** * EXTERNAL -- the connector's "constructor" ***********************************************************************/extern CONNECTOR HTTP_CreateConnector(const SConnNetInfo* info, const char* user_header, THCC_Flags flags){ SConnNetInfo *net_info; CONNECTOR connector; net_info = info ? ConnNetInfo_Clone(info) : ConnNetInfo_Create(0); if (user_header) ConnNetInfo_SetUserHeader(net_info, user_header); connector = HTTP_CreateConnectorEx(net_info, flags, 0, 0, 0, 0); ConnNetInfo_Destroy(net_info); return connector;}extern CONNECTOR HTTP_CreateConnectorEx(const SConnNetInfo* net_info, THCC_Flags flags, FHttpParseHTTPHeader parse_http_hdr, FHttpAdjustNetInfo adjust_net_info, void* adjust_data, FHttpAdjustCleanup adjust_cleanup){ CONNECTOR ccc = (SConnector *) malloc(sizeof(SConnector )); SHttpConnector* uuu = (SHttpConnector*) malloc(sizeof(SHttpConnector)); /* initialize internal data structure */ uuu->net_info = net_info ? ConnNetInfo_Clone(net_info) : ConnNetInfo_Create(0); ConnNetInfo_AdjustForHttpProxy(uuu->net_info); if (uuu->net_info->debug_printout) ConnNetInfo_Log(uuu->net_info, CORE_GetLOG()); uuu->parse_http_hdr = parse_http_hdr; uuu->adjust_net_info = adjust_net_info; uuu->adjust_cleanup = adjust_cleanup; uuu->adjust_data = adjust_data; uuu->flags = flags; if (flags & fHCC_UrlDecodeInput) uuu->flags &= ~fHCC_KeepHeader; uuu->can_connect = eCC_Once; /* will be properly set at open */ uuu->error_header = getenv("HTTP_ERROR_HEADER_ONLY") ? 1 : 0; uuu->sock = 0; uuu->o_timeout = kDefaultTimeout; /* deliberately bad values -- */ uuu->w_timeout = kDefaultTimeout; /* will be reset prior to use */ uuu->http = 0; uuu->r_buf = 0; uuu->w_buf = 0; /* there are some unintialized fields left -- they are initted later */ /* initialize connector structure */ ccc->handle = uuu; ccc->next = 0; ccc->meta = 0; ccc->setup = s_Setup; ccc->destroy = s_Destroy; return ccc;}/* * -------------------------------------------------------------------------- * $Log: ncbi_http_connector.c,v $ * Revision 1000.3 2004/02/12 21:52:44 gouriano * PRODUCTION: UPGRADED [CORE_001] Dev-tree R6.63 * * Revision 6.63 2004/02/12 16:50:02 lavr * Heed warning about uninited variable use * * Revision 6.62 2003/11/26 12:57:11 lavr * s_ReadHeader(): check header size first before looking for end-of-header * * Revision 6.61 2003/11/03 17:37:42 lavr * Fix previous accidental commit and provide corrent change log info: * 1. Added more notes about SOCK_Shutdown() being left commented out; * 2. Do not print HTTP error body if data trace mode set to "DATA". * * Revision 6.59 2003/10/29 14:09:08 lavr * Log levels and messages changed in some error reports * * Revision 6.58 2003/10/23 12:27:38 lavr * Do not double HTTP header when full data logging is explicitly on * * Revision 6.57 2003/10/07 20:00:10 lavr * Remove SOCK_Shutdown() after sending the HTTP header and request body * * Revision 6.56 2003/09/30 19:44:08 lavr * Fix typos in error messages * * Revision 6.55 2003/08/25 14:40:29 lavr * Employ new k..Timeout constants * * Revision 6.54 2003/06/09 19:52:42 lavr * New env.var. HTTP_ERROR_HEADER_ONLY to control header output on SOME tracing * * Revision 6.53 2003/06/04 20:58:13 lavr * s_Adjust() to return failure if no adjustment callback specified * s_VT_Status() to return eIO_Closed if connector failed/closed * * Revision 6.52 2003/05/31 05:15:15 lavr * Add ARGSUSED where args are meant to be unused * * Revision 6.51 2003/05/21 17:54:16 lavr * s_VT_Read() to return eIO_Success if some data have been read * * Revision 6.50 2003/05/20 23:54:00 lavr * Reinstate ConnNetInfo_Destroy() accidently deleted from s_Destroy() * * Revision 6.49 2003/05/20 21:26:40 lavr * Restructure SHttpConnector; add SHttpConnector::shut_down; enable to * call SOCK_Shutdown() again (after doing a dummy write of 0 bytes) * * Revision 6.48 2003/05/19 21:03:50 lavr * Remove SOCK_Shutdown() temporarily * * Revision 6.47 2003/05/19 16:48:39 lavr * Pending HTTP body write implemented ({0,0}-timeout tolerant) * * Revision 6.46 2003/05/14 03:57:48 lavr * Better logging; implementation of CONN_Description(); support of * {0,0} connect timeouts; bug workaround of MSVC's lame shutdown() is * now in ncbi_socket.c and moved away from this module * * Revision 6.45 2003/04/30 17:01:42 lavr * One (unnecessary) variable removed from s_ReadHeader() * * Revision 6.44 2003/02/04 22:04:11 lavr * Minor fix in comment * * Revision 6.43 2003/01/31 21:18:49 lavr * Fullfil max tries even in the absence of connection adjustment routine * * Revision 6.42 2003/01/17 19:44:46 lavr * Reduce dependencies * * Revision 6.41 2003/01/15 20:27:29 lavr * Fix breeding of NCBIHttpConnector token in User-Agent: header tag * * Revision 6.40 2003/01/10 14:51:29 lavr * Revert to R6.37 but properly handle drop of w_buf in s_FlushAndDisconnect() * * Revision 6.37 2002/12/13 21:19:40 lavr * Extend User-Agent: header tag * * Revision 6.36 2002/11/19 19:20:37 lavr * Server error parsing slightly changed * * Revision 6.35 2002/10/28 15:46:20 lavr * Use "ncbi_ansi_ext.h" privately * * Revision 6.34 2002/10/22 15:11:24 lavr * Zero connector's handle to crash if revisited * * Revision 6.33 2002/09/06 15:46:45 lavr * Few bug fixes about corner cases of I/O waiting, flushing and logging * * Revision 6.32 2002/08/12 15:12:23 lavr * Use persistent SOCK_Write() * * Revision 6.31 2002/08/07 16:33:04 lavr * Changed EIO_ReadMethod enums accordingly; log moved to end * * Revision 6.30 2002/07/25 20:20:43 lavr * Do not report header read error on {0, 0} timeout * * Revision 6.29 2002/07/25 13:59:35 lavr * More diagnostic messages added * * Revision 6.28 2002/06/19 18:08:02 lavr * Fixed some wrong assumptions on use of s_PreRead(); more comments added * * Revision 6.27 2002/06/10 19:51:20 lavr * Small prettifying * * Revision 6.26 2002/05/06 19:13:48 lavr * Output to stderr replaced with calls to logger * * Revision 6.25 2002/04/26 16:36:56 lavr * Added setting of default timeout in meta-connector's setup routine * Remove all checks for kDefaultTimeout: now supplied good from CONN * * Revision 6.24 2002/04/22 19:31:33 lavr * Reading/waiting redesigned to be more robust in case of network errors * * Revision 6.23 2002/03/22 19:52:16 lavr * Do not include <stdio.h>: included from ncbi_util.h or ncbi_priv.h * * Revision 6.22 2002/02/11 20:36:44 lavr * Use "ncbi_config.h" * * Revision 6.21 2002/02/05 22:04:12 lavr * Included header files rearranged * * Revision 6.20 2001/12/31 14:53:46 lavr * #include <connect/ncbi_ansi_ext.h> for Mac to be happy (noted by J.Kans) * * Revision 6.19 2001/12/30 20:00:00 lavr * Redirect on non-empty location only * * Revision 6.18 2001/12/30 19:41:07 lavr * Process error codes 301 and 302 (document moved) and reissue HTTP request * * Revision 6.17 2001/09/28 20:48:23 lavr * Comments revised; parameter (and SHttpConnector's) names adjusted * Retry logic moved entirely into s_Adjust() * * Revision 6.16 2001/09/10 21:15:56 lavr * Readability issue: FParseHTTPHdr -> FParseHTTPHeader * * Revision 6.15 2001/05/23 21:52:44 lavr * +fHCC_NoUpread * * Revision 6.14 2001/05/17 15:02:51 lavr * Typos corrected * * Revision 6.13 2001/05/08 20:26:27 lavr * Patches in re-try code * * Revision 6.12 2001/04/26 20:21:34 lavr * Reorganized and and made slightly more effective * * Revision 6.11 2001/04/24 21:41:47 lavr * Reorganized code to allow reconnects in case of broken connections at * stage of connection, sending data and receiving HTTP header. Added * code to pull incoming data from connection while sending - stall protection. * * Revision 6.10 2001/03/02 20:08:47 lavr * Typo fixed * * Revision 6.9 2001/01/25 16:53:24 lavr * New flag for HTTP_CreateConnectorEx: fHCC_DropUnread * * Revision 6.8 2001/01/23 23:11:20 lavr * Status virtual method implemented * * Revision 6.7 2001/01/11 16:38:17 lavr * free(connector) removed from s_Destroy function * (now always called from outside, in METACONN_Remove) * * Revision 6.6 2001/01/03 22:32:43 lavr * Redundant calls to 'adjust_info' removed * * Revision 6.5 2000/12/29 17:57:16 lavr * Adapted for use of new connector structure; * parse header callback added; some internal functions renamed. * * Revision 6.4 2000/11/15 18:52:02 vakatov * Call SOCK_Shutdown() after the HTTP request is sent. * Use SOCK_Status() instead of SOCK_Eof(). * * Revision 6.3 2000/10/12 21:43:14 vakatov * Minor cosmetic fix... * * Revision 6.2 2000/09/26 22:02:57 lavr * HTTP request method added * * Revision 6.1 2000/04/21 19:41:01 vakatov * Initial revision * * ========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -