📄 ncbi_connutil.h
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbi_connutil.h,v $ * PRODUCTION Revision 1000.1 2004/02/12 21:51:37 gouriano * PRODUCTION PRODUCTION: UPGRADED [CORE_001] Dev-tree R6.35 * PRODUCTION * =========================================================================== */#ifndef CONNECT___NCBI_CONNUTIL__H#define CONNECT___NCBI_CONNUTIL__H/* $Id: ncbi_connutil.h,v 1000.1 2004/02/12 21:51:37 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov, Anton Lavrentiev * * File Description: * Auxiliary API to: * 1.Retrieve connection related info from the registry: * SConnNetInfo * ConnNetInfo_Create() * ConnNetInfo_AdjustForHttpProxy() * ConnNetInfo_Clone() * ConnNetInfo_Print() * ConnNetInfo_Destroy() * ConnNetInfo_Log() * ConnNetInfo_ParseURL() * ConnNetInfo_SetUserHeader() * ConnNetInfo_AppendUserHeader() * ConnNetInfo_DeleteUserHeader() * ConnNetInfo_OverrideUserHeader() * ConnNetInfo_ExtendUserHeader() * ConnNetInfo_AppendArg() * ConnNetInfo_PrependArg() * ConnNetInfo_DeleteArg() * ConnNetInfo_PreOverrideArg() * ConnNetInfo_PostOverrideArg() * #define REG_CONN_*** * #define DEF_CONN_*** * * 2.Make a connection to an URL: * URL_Connect() * * 3.Perform URL encoding/decoding of data: * URL_Decode() * URL_DecodeEx() * URL_Encode() * * 4.Compose or parse NCBI-specific Content-Type's: * EMIME_SubType * EMIME_Encoding * MIME_ComposeContentType() * MIME_ParseContentType() * * 5.Search for a token in the input stream (either CONN or SOCK): * CONN_StripToPattern() * SOCK_StripToPattern() * BUF_StripToPattern() * * 6.Convert "[host][:port]" from verbal into binary form and vice versa: * StringToHostPort() * HostPortToString() * */#include <connect/ncbi_buffer.h>#include <connect/ncbi_connection.h>#include <connect/ncbi_socket.h>/** @addtogroup UtilityFunc * * @{ */#ifdef __cplusplusextern "C" {#endiftypedef enum { eReqMethod_Any = 0, eReqMethod_Post, eReqMethod_Get} EReqMethod;typedef enum { eDebugPrintout_None = 0, eDebugPrintout_Some, eDebugPrintout_Data} EDebugPrintout;/* Network connection related configurable info struct. * ATTENTION: Do NOT fill out this structure (SConnNetInfo) "from scratch"! * Instead, use ConnNetInfo_Create() described below to create * it, and then fix (hard-code) some fields, if really necessary. */typedef struct { char client_host[256]; /* effective client hostname */ char host[256]; /* host to connect to */ unsigned short port; /* port to connect to, host byte order */ char path[1024]; /* service: path(e.g. to a CGI script) */ char args[1024]; /* service: args(e.g. for a CGI script) */ EReqMethod req_method; /* method to use in the request */ STimeout* timeout; /* ptr to i/o tmo (infinite if NULL) */ unsigned short max_try; /* max. # of attempts to connect (>= 1) */ char http_proxy_host[256]; /* hostname of HTTP proxy server */ unsigned short http_proxy_port; /* port # of HTTP proxy server */ char proxy_host[256]; /* CERN-like (non-transp) f/w proxy srv */ EDebugPrintout debug_printout; /* printout some debug info */ int/*bool*/ stateless; /* to connect in HTTP-like fashion only */ int/*bool*/ firewall; /* to use firewall/relay in connects */ int/*bool*/ lb_disable; /* to disable local load-balancing */ const char* http_user_header; /* user header to add to HTTP request */ /* the following field(s) are for the internal use only! */ int/*bool*/ http_proxy_adjusted; STimeout tmo; /* default storage for finite timeout */ const char* service; /* service for which this info created */} SConnNetInfo;/* Defaults and the registry entry names for "SConnNetInfo" fields */#define DEF_CONN_REG_SECTION "CONN"#define REG_CONN_HOST "HOST"#define DEF_CONN_HOST "www.ncbi.nlm.nih.gov"#define REG_CONN_PORT "PORT"#define DEF_CONN_PORT 80#define REG_CONN_PATH "PATH"#define DEF_CONN_PATH "/Service/dispd.cgi"#define REG_CONN_ARGS "ARGS"#define DEF_CONN_ARGS ""#define REG_CONN_REQ_METHOD "REQ_METHOD"#define DEF_CONN_REQ_METHOD "POST"#define REG_CONN_TIMEOUT "TIMEOUT"#define DEF_CONN_TIMEOUT 30.0#define REG_CONN_MAX_TRY "MAX_TRY"#define DEF_CONN_MAX_TRY 3#define REG_CONN_HTTP_PROXY_HOST "HTTP_PROXY_HOST"#define DEF_CONN_HTTP_PROXY_HOST ""#define REG_CONN_HTTP_PROXY_PORT "HTTP_PROXY_PORT"#define DEF_CONN_HTTP_PROXY_PORT 80#define REG_CONN_PROXY_HOST "PROXY_HOST"#define DEF_CONN_PROXY_HOST ""#define REG_CONN_DEBUG_PRINTOUT "DEBUG_PRINTOUT"#define DEF_CONN_DEBUG_PRINTOUT ""#define REG_CONN_STATELESS "STATELESS"#define DEF_CONN_STATELESS ""#define REG_CONN_FIREWALL "FIREWALL"#define DEF_CONN_FIREWALL ""#define REG_CONN_LB_DISABLE "LB_DISABLE"#define DEF_CONN_LB_DISABLE ""/* This function to fill out the "*info" structure using * registry entries named (see above) in macros REG_CONN_<NAME>: * * -- INFO FIELD -- ----- NAME ----- ---------- REMARKS/EXAMPLES --------- * client_host local host name assigned automatically * service_name SERVICE_NAME no search/no value without service * host HOST * port PORT * path PATH * args ARGS * req_method REQ_METHOD * timeout TIMEOUT "<sec>.<usec>": "3.00005", "infinite" * max_try MAX_TRY * http_proxy_host HTTP_PROXY_HOST no HTTP proxy if empty/NULL * http_proxy_port HTTP_PROXY_PORT * proxy_host PROXY_HOST * debug_printout DEBUG_PRINTOUT * client_mode CLIENT_MODE * lb_disable LB_DISABLE * * A value of the field NAME is first looked for in the environment variable * of the form service_CONN_NAME; then in the current corelib registry, * in the section 'service' by using key CONN_NAME; then in the environment * variable again, but using the name CONN_NAME; and finally in the default * registry section (DEF_CONN_REG_SECTION), using just NAME. If service * is NULL or empty then the first 2 steps in the above lookup are skipped. * * For default values see right above, in macros DEF_CONN_<NAME>. */extern NCBI_XCONNECT_EXPORT SConnNetInfo* ConnNetInfo_Create(const char* service );/* Adjust the "host:port" to "proxy_host:proxy_port", and * "path" to "http://host:port/path" to connect through a HTTP proxy. * Return FALSE if already adjusted(see the NOTE), or if cannot adjust * (e.g. if "host" + "path" are too long). * NOTE: it does nothing if applied more than once to the same "info" * (or its clone), or when "http_proxy_host" is NULL. */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_AdjustForHttpProxy(SConnNetInfo* info );/* Make an exact and independent copy of "*info". */extern NCBI_XCONNECT_EXPORT SConnNetInfo* ConnNetInfo_Clone(const SConnNetInfo* info );/* Convenience routines to manipulate SConnNetInfo::args[]. * In "arg" all routines below assume to have a single arg name * or an "arg=value" pair. In the former case, additional "val" * may be supplied separately (and will be prepended by "=" if * necessary). In the latter case, having a non-zero string in * "val" may result in an erroneous behavior. Ampersand (&) gets * automatically added to keep the arg list correct. * Return value (if any): none-zero on success; 0 on error. *//* append argument to the end of the list */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_AppendArg(SConnNetInfo* info, const char* arg, const char* val );/* put argument in the front of the list */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_PrependArg(SConnNetInfo* info, const char* arg, const char* val );/* delete argument from the list */extern NCBI_XCONNECT_EXPORT void ConnNetInfo_DeleteArg(SConnNetInfo* info, const char* arg );/* same as sequence Delete then Prepend, see above */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_PreOverrideArg(SConnNetInfo* info, const char* arg, const char* val );/* same as sequence Delete then Append, see above */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_PostOverrideArg(SConnNetInfo* info, const char* arg, const char* val );/* Set user header (discard previously set header, if any). * Reset the old header (if any) if "header" == NULL. * Return non-zero if successful, otherwise return 0 to indicate an error. */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_SetUserHeader(SConnNetInfo* info, const char* header );/* Append user header (same as ConnNetInfo_SetUserHeader() if no previous * header was set, or if "header" == NULL). * Return non-zero if successful, otherwise return 0 to indicate an error. */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_AppendUserHeader(SConnNetInfo* info, const char* header );/* Override user header. * Tags replaced (case-insensitively), and tags with empty values effectively * delete existing tags from the old user header, e.g. "My-Tag:\r\n" deletes * any appearence (if any) of "My-Tag: [<value>]" from the user header. * Unmatched tags with non-empty values are simply added to the existing user * header (as with "Append" above). * Return non-zero if successful, otherwise return 0 to indicate an error. */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_OverrideUserHeader(SConnNetInfo* info, const char* header );/* Extend user header. * Existings tags matching (case-insensitively) those from "header" are * appended with new value (separated by a comma and a space) if the added * value is non-empty, otherwise, the tags are left untouched. All new * unmatched tags from "header" with non-empty values get added to the end * of the user header. * Return non-zero if successful, otherwise return 0 to indicate an error. */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_ExtendUserHeader(SConnNetInfo* info, const char* header );/* Delete entries from current user header, if their tags match those * passed in "hdr" (regardless of the values, if any, in the latter). */extern NCBI_XCONNECT_EXPORT void ConnNetInfo_DeleteUserHeader(SConnNetInfo* info, const char* hdr );/* Parse URL into "*info", using (service-specific, if any) defaults. */extern NCBI_XCONNECT_EXPORT int/*bool*/ ConnNetInfo_ParseURL(SConnNetInfo* info, const char* url );/* Log the contents of "*info". */extern NCBI_XCONNECT_EXPORT void ConnNetInfo_Log(const SConnNetInfo* info, LOG log );/* Destroy and deallocate "info" (if not NULL). */extern NCBI_XCONNECT_EXPORT void ConnNetInfo_Destroy(SConnNetInfo* info);/* Hit URL "http://host:port/path?args" with: * {POST|GET} <path>?<args> HTTP/1.0\r\n * <user_header\r\n> * Content-Length: <content_length>\r\n\r\n * If "encode_args" is TRUE then URL-encode the "args". * "args" can be NULL/empty -- then the '?' symbol does not get added. * The "content_length" is mandatory, and it specifies an exact(!) amount of * data that you are planning to send to the resultant socket (0 if none). * If string "user_header" is not NULL/empty, then it must be terminated by a * single '\r\n'. * * On success, return non-NULL handle of a socket. * ATTENTION: due to the very essence of the HTTP connection, you may * perform only one { WRITE, ..., WRITE, READ, ..., READ } cycle. * Returned socket must be closed exipicitly by "ncbi_socket.h:SOCK_Close()" * when no longer needed. * On error, return NULL. * * NOTE: Returned socket may not be immediately readable/writeable if open
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -