📄 ncbi_connutil.c
字号:
extern char* MIME_ComposeContentTypeEx(EMIME_Type type, EMIME_SubType subtype, EMIME_Encoding encoding, char* buf, size_t buflen){ static const char s_ContentType[] = "Content-Type: "; const char* x_Type = s_MIME_Type [(int) type]; const char* x_SubType = s_MIME_SubType [(int) subtype]; const char* x_Encoding = s_MIME_Encoding[(int) encoding]; char x_buf[MAX_CONTENT_TYPE_LEN]; if ( *x_Encoding ) { assert(sizeof(s_ContentType) + strlen(x_Type) + strlen(x_SubType) + strlen(x_Encoding) + 4 < MAX_CONTENT_TYPE_LEN); sprintf(x_buf, "%s%s/%s-%s\r\n", s_ContentType, x_Type, x_SubType, x_Encoding); } else { assert(sizeof(s_ContentType) + strlen(x_Type) + strlen(x_SubType) + 3 < MAX_CONTENT_TYPE_LEN); sprintf(x_buf, "%s%s/%s\r\n", s_ContentType, x_Type, x_SubType); } assert(strlen(x_buf) < sizeof(x_buf)); assert(strlen(x_buf) < buflen); strncpy0(buf, x_buf, buflen - 1); return buf;}extern char* MIME_ComposeContentType(EMIME_SubType subtype, EMIME_Encoding encoding, char* buf, size_t buflen){ return MIME_ComposeContentTypeEx(eMIME_T_NcbiData, subtype, encoding, buf, buflen);}extern int/*bool*/ MIME_ParseContentTypeEx(const char* str, EMIME_Type* type, EMIME_SubType* subtype, EMIME_Encoding* encoding){ char* x_buf; char* x_type; char* x_subtype; int i; if ( type ) *type = eMIME_T_Unknown; if ( subtype ) *subtype = eMIME_Unknown; if ( encoding ) *encoding = eENCOD_Unknown; if (!str || !*str) return 0/*false*/; {{ size_t x_size = strlen(str) + 1; x_buf = (char*) malloc(2 * x_size); x_type = x_buf + x_size; }} strcpy(x_buf, str); strlwr(x_buf); if ((sscanf(x_buf, " content-type: %s ", x_type) != 1 && sscanf(x_buf, " %s ", x_type) != 1) || (x_subtype = strchr(x_type, '/')) == 0) { free(x_buf); return 0/*false*/; } *x_subtype++ = '\0'; if ( type ) { for (i = 0; i < (int) eMIME_T_Unknown; i++) { if ( !strcmp(x_type, s_MIME_Type[i]) ) { *type = (EMIME_Type) i; break; } } } for (i = 0; i < (int) eENCOD_Unknown; i++) { char* x_encoding = strstr(x_subtype, s_MIME_Encoding[i]); if (x_encoding && *x_encoding && x_encoding != x_subtype && *(x_encoding - 1) == '-' && strcmp(x_encoding, s_MIME_Encoding[i]) == 0) { if ( encoding ) { *encoding = (EMIME_Encoding) i; } *(x_encoding - 1) = '\0'; break; } } if (encoding && *encoding == eENCOD_Unknown) *encoding = eENCOD_None; if ( subtype ) { for (i = 0; i < (int) eMIME_Unknown; i++) { if ( !strcmp(x_subtype, s_MIME_SubType[i]) ) { *subtype = (EMIME_SubType) i; break; } } } free(x_buf); return 1/*true*/;}extern int/*bool*/ MIME_ParseContentType(const char* str, EMIME_SubType* subtype, EMIME_Encoding* encoding){ EMIME_Type type; if ( !MIME_ParseContentTypeEx(str, &type, subtype, encoding) ) return 0/*false*/; if (type != eMIME_T_NcbiData) { if ( subtype ) *subtype = eMIME_Unknown; if ( encoding ) *encoding = eENCOD_Unknown; return 0/*false*/; } return 1/*true*/;}/**************************************************************************** * Reading and writing [host][:port] addresses */extern const char* StringToHostPort(const char* str, unsigned int* host, unsigned short* port){ unsigned short p; unsigned int h; char abuf[256]; const char* s; size_t alen; int n = 0; *host = 0; *port = 0; for (s = str; *s; s++) { if (isspace((unsigned char)(*s)) || *s == ':') break; } if ((alen = (size_t)(s - str)) > sizeof(abuf) - 1) return str; if (alen) { strncpy0(abuf, str, alen); if (!(h = SOCK_gethostbyname(abuf))) return str; } else h = 0; if (*s == ':') { if (sscanf(++s, "%hu%n", &p, &n) < 1 || (s[n] && !isspace((unsigned char) s[n]))) return alen ? 0 : str; } else p = 0; *host = h; *port = p; return s + n;}extern size_t HostPortToString(unsigned int host, unsigned short port, char* buf, size_t buflen){ char x_buf[16/*sizeof("255.255.255.255")*/ + 8/*:port*/]; size_t n; if (!buf || !buflen) return 0; if (!host) *x_buf = 0; else if (SOCK_ntoa(host, x_buf, sizeof(x_buf)) != 0) { *buf = 0; return 0; } n = strlen(x_buf); if (port || !host) n += sprintf(x_buf + n, ":%hu", port); assert(n < sizeof(x_buf)); if (n >= buflen) n = buflen - 1; memcpy(buf, x_buf, n); buf[n] = 0; return n;}/* * -------------------------------------------------------------------------- * $Log: ncbi_connutil.c,v $ * Revision 1000.3 2004/04/12 17:06:12 gouriano * PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R6.64 * * Revision 6.64 2004/04/06 19:25:56 lavr * Fix ConnNetInfo_DeleteArg() to remove arg's trailing '&' * * Revision 6.63 2004/01/14 18:52:39 lavr * Recognize eMIME_XmlSoap and corresponding text representation "xml+soap" * * Revision 6.62 2004/01/07 19:23:29 lavr * "xml" added as a MIME subtype * * Revision 6.61 2003/11/12 17:46:12 lavr * HostPortToString() changed to be a little more efficient * * Revision 6.60 2003/08/27 16:27:37 lavr * Add "Host:" tag to be able to take advantage of Apache VHosts * * Revision 6.59 2003/08/25 14:44:43 lavr * Employ new k..Timeout constants * URL_Connect(): get rid of one avoidable malloc() * User header manipulation routines: factor out some arithmetics for speed * * Revision 6.58 2003/05/31 05:13:38 lavr * Replace bitwise XOR with inequality [of the same effect] * * Revision 6.57 2003/05/20 21:25:24 lavr * Limit SConnNetInfo::max_try by reasonable "short" value * * Revision 6.56 2003/05/19 16:44:45 lavr * Minor style adjustements * * Revision 6.55 2003/05/14 03:51:54 lavr * URL_Connect() rewritten to submit HTTP header as SOCK's initial buffer * * Revision 6.54 2003/04/30 17:02:11 lavr * Name collision resolved in ConnNetInfo_ParseURL() * * Revision 6.53 2003/03/06 21:55:31 lavr * s_ModifyUserHeader(): Heed uninitted usage warning * HostPortToString(): Do not append :0 (for zero port) if host is not empty * * Revision 6.52 2003/02/28 14:47:41 lavr * Bugfix: proper bool -> eIO_Status conversion in s_BUF_IO() * * Revision 6.51 2003/01/31 21:17:04 lavr * More robust search for sheme in URL * * Revision 6.50 2003/01/17 19:44:46 lavr * Reduce dependencies * * Revision 6.49 2003/01/15 19:52:25 lavr * *_StripToPattern() calls modified to use Read/PushBack instead of Peek/Read * * Revision 6.48 2002/12/13 21:19:13 lavr * Separate header tag values with spaces as most commonly required (rfc1945) * * Revision 6.47 2002/12/10 17:34:15 lavr * Remove errno decoding on failed connect in URL_Connect() * * Revision 6.46 2002/12/05 21:43:31 lavr * Fix in assignment and compare in URL_Connect() * * Revision 6.45 2002/12/04 16:50:47 lavr * Use SOCK_CreateEx() in URL_Connect() * * Revision 6.44 2002/11/19 19:19:57 lavr * +ConnNetInfo_ExtendUserHeader() * * Revision 6.43 2002/11/13 19:54:13 lavr * ConnNetInfo_DeleteArg(): fix initial argument calculation size * * Revision 6.42 2002/11/12 05:50:33 lavr * Modify client host name discovery * * Revision 6.41 2002/11/01 20:13:50 lavr * Remove MAXHOSTNAMELEN and MAX_IP_ADDR_LEN macros * * Revision 6.40 2002/10/28 15:42:38 lavr * Use "ncbi_ansi_ext.h" privately and use strncpy0() * * Revision 6.39 2002/10/21 18:30:59 lavr * +ConnNetInfo_AppendArg() * +ConnNetInfo_PrependArg() * +ConnNetInfo_DeleteArg() * +ConnNetInfo_PreOverrideArg() * +ConnNetInfo_PostOverrideArg() * * Revision 6.38 2002/10/11 19:42:06 lavr * +ConnNetInfo_AppendUserHeader() * +ConnNetInfo_OverrideUserHeader() * +ConnNetInfo_DeleteUserHeader() * * Revision 6.37 2002/09/24 18:08:34 lavr * Avoid compiler warning in positioning on first but one array element * * Revision 6.36 2002/08/12 15:12:01 lavr * Use persistent SOCK_Write() * * Revision 6.35 2002/08/07 16:32:47 lavr * Changed EIO_ReadMethod enums accordingly; log moved to end * * Revision 6.34 2002/05/06 19:12:57 lavr * -ConnNetInfo_Print(); +ConnNetInfo_Log() * Addition: *_StripToPattern() now can strip until EOF (or error) * * Revision 6.33 2002/04/26 16:31:41 lavr * Add more space between functions to separate them better * * Revision 6.32 2002/04/13 06:36:36 lavr * Fix for empty path parsing in HTTP URL * * Revision 6.31 2002/03/19 22:13:09 lavr * Minor tweak in recognizing "infinite" (and part) as a special timeout * * Revision 6.30 2002/03/11 21:53:18 lavr * Recognize ALL in CONN_DEBUG_PRINTOUT; bugfix for '//' in proxy adjustement * * Revision 6.29 2002/02/20 19:12:17 lavr * Swapped eENCOD_Url and eENCOD_None; eENCOD_Unknown introduced * * Revision 6.28 2002/02/08 22:22:17 lavr * BUGFIX: sizeof(info) -> sizeof(*info) in ConnNetInfo_Create() * * Revision 6.27 2002/01/30 20:14:48 lavr * URL_Connect(): Print error code in some failure messages * * Revision 6.26 2002/01/28 20:21:46 lavr * Do not store "" as a user_header * * Revision 6.25 2001/12/30 19:40:32 lavr * +ConnNetInfo_ParseURL() * Added recordkeeping of service name for which the info was created * * Revision 6.24 2001/12/04 15:56:28 lavr * Use strdup() instead of explicit strcpy(malloc(...), ...) * * Revision 6.23 2001/09/24 20:27:00 lavr * Message corrected: "Adjusted path too long" * * Revision 6.22 2001/09/10 21:14:58 lavr * Added functions: StringToHostPort() * HostPortToString() * * Revision 6.21 2001/05/31 21:30:57 vakatov * MIME_ParseContentTypeEx() -- a more accurate parsing * * Revision 6.20 2001/05/29 21:15:43 vakatov * + eMIME_Plain * * Revision 6.19 2001/04/24 21:29:43 lavr * Special text value "infinite" accepted as infinite timeout from environment * * Revision 6.18 2001/03/26 18:37:09 lavr * #include <ctype.h> not used, removed * * Revision 6.17 2001/03/02 20:08:05 lavr * Typo fixed * * Revision 6.16 2001/01/25 16:58:33 lavr * ConnNetInfo_SetUserHeader now used throughout to set/reset http_user_header * * Revision 6.15 2001/01/23 23:06:18 lavr * SConnNetInfo.debug_printout converted from boolean to enum * BUF_StripToPattern() introduced * * Revision 6.14 2001/01/12 00:01:27 lavr * CONN_PROXY_HOST was forgotten to init in ConnNetInfo_Create * * Revision 6.13 2001/01/11 23:07:08 lavr * ConnNetInfo_Print() prints user-header 'as is'; pretty-printing undone * * Revision 6.12 2001/01/08 23:46:27 lavr * REQUEST_METHOD -> REQ_METHOD to be consistent with SConnNetInfo * * Revision 6.11 2001/01/08 22:35:56 lavr * Client-Mode removed; replaced by 2 separate boolean fields: * stateless and firewall * * Revision 6.10 2000/12/29 17:54:11 lavr * NCBID stuff removed; ConnNetInfo_SetUserHeader added; * modifications to ConnNetInfo_Print output. * * Revision 6.9 2000/11/07 23:23:18 vakatov * In-sync with the C Toolkit "connutil.c:R6.15", "connutil.h:R6.13" * (with "eMIME_Dispatch" added). * * Revision 6.8 2000/10/20 17:08:40 lavr * All keys capitalized for registry access * Search for some keywords made case insensitive * * Revision 6.7 2000/10/11 22:29:44 lavr * Forgotten blank added after {GET|POST} request in URL_Connect * * Revision 6.6 2000/10/05 22:35:24 lavr * SConnNetInfo modified to contain 'client_mode' instead of just * 'firewall' boolean * * Revision 6.5 2000/09/27 19:37:40 lavr * ncbi_ansi_ext.h included * * Revision 6.4 2000/09/26 22:01:33 lavr * Registry entries changed, HTTP request method added * * Revision 6.3 2000/04/21 19:42:35 vakatov * Several minor typo/bugs fixed * * Revision 6.2 2000/03/29 16:36:09 vakatov * MIME_ParseContentType() -- a fix * * Revision 6.1 2000/03/24 22:53:34 vakatov * Initial revision * * ========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -