📄 httptrans.c
字号:
/*************************************************************************//* module: Communication Services, HTTP functions *//* file: src/xpt/bindings/http/all/httptrans.c *//* target system: all *//* target OS: all *//*************************************************************************//* * Copyright Notice * Copyright (c) Ericsson, IBM, Lotus, Matsushita Communication * Industrial Co., Ltd., Motorola, Nokia, Openwave Systems, Inc., * Palm, Inc., Psion, Starfish Software, Symbian, Ltd. (2001). * All Rights Reserved. * Implementation of all or part of any Specification may require * licenses under third party intellectual property rights, * including without limitation, patent rights (such a third party * may or may not be a Supporter). The Sponsors of the Specification * are not responsible and shall not be held responsible in any * manner for identifying or failing to identify any or all such * third party intellectual property rights. * * THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN ARE PROVIDED * ON AN "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND AND ERICSSON, IBM, * LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO. LTD, MOTOROLA, * NOKIA, PALM INC., PSION, STARFISH SOFTWARE AND ALL OTHER SYNCML * SPONSORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT * SHALL ERICSSON, IBM, LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO., * LTD, MOTOROLA, NOKIA, PALM INC., PSION, STARFISH SOFTWARE OR ANY * OTHER SYNCML SPONSOR BE LIABLE TO ANY PARTY FOR ANY LOSS OF * PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTAL, * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH * THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE. * * The above notice and this paragraph must be included on all copies * of this document that are made. * */#include "syncml_tk_prefix_file.h" // %%% luz: needed for precompiled headers in eVC++#include <httpdefs.h>#include <httptrans.h>// %%% luz: required for sprintf#include <stdio.h>#include "httpserverports.h"#ifdef __EPOC_OS__#include "http_globals_epoc.h"#endif#ifndef __EPOC_OS__static HttpTransportInfo_t transportInfo;#endif#ifdef __EPOC_OS__#define transportInfo TheHttpGlobalsEpoc()->transportInfo#endif#ifdef __PALM_OS__ #define sprintf StrPrintF#endif/*---------------------------------*/#ifdef LINK_TRANSPORT_STATICALLY #define initializeTransport HTTP_initializeTransport#endifXPTEXP1 Ret_t XPTAPI XPTEXP2 initializeTransport() { struct xptTransportDescription desc;#ifdef __EPOC_OS__ Ret_t err; err = httpOpenTLS(); if (err != SML_ERR_OK) return SML_ERR_A_XPT_MEMORY;#endif desc.shortName = "HTTP"; desc.description = "HyperText Transfer Protocol over TCP"; desc.flags = XPT_CLIENT | XPT_SERVER; desc.privateTransportInfo = &transportInfo; // Transport Info desc.selectProtocol = HTTP_selectProtocol; desc.deselectProtocol = HTTP_deselectProtocol; desc.openCommunication = HTTP_openCommunication; desc.closeCommunication = HTTP_closeCommunication; desc.beginExchange = HTTP_beginExchange; desc.endExchange = HTTP_endExchange; desc.receiveData = HTTP_receiveData; desc.sendData = HTTP_sendData; desc.sendComplete = HTTP_sendComplete; desc.setDocumentInfo = HTTP_setDocumentInfo; desc.getDocumentInfo = HTTP_getDocumentInfo;#ifdef __EPOC_OS__ desc.resetBindingTLS = httpCloseTLS;#endif transportInfo.cbSize = sizeof( transportInfo ); initializeServerPorts(); return xptRegisterTransport( &desc );}void setError( unsigned long rc, const char *msg ) { struct xptTransportErrorInformation err; err.protocolErrorCode = rc; err.errorMessage = msg; xptSetLastError( &err );}void getFirewallInfo( TcpFirewallInfoPtr_t firewall, const char *buffer, int len ) { char *p; firewall->serverName = xppMalloc( len + 1 ); xppMemcpy( firewall->serverName, buffer, len ); firewall->serverName[len] = '\0'; p = xppStrchr( firewall->serverName, ':' ); if (p) { *p = '\0'; firewall->serverPort = xppAtoi( p+1 ); }}Ret_t XPTAPI HTTP_selectProtocol(void *privateTransportInfo, const char *metaInformation, unsigned int flags, void **pPrivateServiceInfo) { Ret_t ret = SML_ERR_OK; TcpRc_t rc; HttpTransportServiceInfoPtr_t info = NULL; if ( privateTransportInfo == NULL || pPrivateServiceInfo == NULL ) { setError( 1, "One of privateTransportInfo or privateServiceInfo pointer was NULL" ); return SML_ERR_A_XPT_INVALID_PARM; } info = ( HttpTransportServiceInfoPtr_t )xppMalloc( sizeof( HttpTransportServiceInfo_t ) ); xppMemset( info, '\0', sizeof( HttpTransportServiceInfo_t ) ); info->cbSize = sizeof( HttpTransportServiceInfo_t ); info->socketClient = (flags & XPT_CLIENT); if ( info->socketClient ) { // initialte a client connection const char *ptr; size_t len; ptr = xptGetMetaInfoValue( metaInformation, "HOST", &len ); if (!ptr) { xppFree(info); setError( 1, "HOST information is not defined in select metaInformation" ); return SML_ERR_A_XPT_COMMUNICATION; } info->info.clientInfo.pchServerAddress = xppMalloc( len + 1 ); xppMemcpy( info->info.clientInfo.pchServerAddress, ptr, len ); info->info.clientInfo.pchServerAddress[len] = '\0'; info->info.clientInfo.firewallInfo.type = TCP_FIREWALL_DIRECT; ptr = xptGetMetaInfoValue( metaInformation, "SOCKSHOST", &len ); if (ptr) { info->info.clientInfo.firewallInfo.type = TCP_FIREWALL_SOCKS; getFirewallInfo( &info->info.clientInfo.firewallInfo, ptr, len ); } else { ptr = xptGetMetaInfoValue( metaInformation, "PROXYHOST", &len ); if (ptr) { info->info.clientInfo.firewallInfo.type = TCP_FIREWALL_PROXY; getFirewallInfo( &info->info.clientInfo.firewallInfo, ptr, len ); // Keep the proxy String, for use later. info->info.clientInfo.proxyString = xppMalloc( len + 1 ); xppMemcpy( info->info.clientInfo.proxyString, ptr, len ); info->info.clientInfo.proxyString[len] = '\0'; } } // %%% luz:2003-04-16 added SSL support // default to no SSL info->info.clientInfo.useSSL=false; ptr = xptGetMetaInfoValue( metaInformation, "SSL", &len ); if (ptr) { if (*ptr!='0') { // SSL requested info->info.clientInfo.useSSL=true; } } } else { // initiate a server connection const char *ptr; size_t len; info->info.serverInfo.pchServerPort = xppMalloc(8); ptr = xptGetMetaInfoValue( metaInformation, "SERVERPORT", &len ); if (!ptr) { xppStrcpy( info->info.serverInfo.pchServerPort, "80" ); } else { if (len >= 8) { xppFree(info); setError( 1, "SERVERPORT information is invalid in select metaInformation" ); return SML_ERR_A_XPT_COMMUNICATION; } xppMemcpy( info->info.serverInfo.pchServerPort, ptr, len ); info->info.serverInfo.pchServerPort[len] = '\0'; } rc = selectServerSocket( info->info.serverInfo.pchServerPort, &info->info.serverInfo.serverSocket ); if ( TCP_RC_OK == rc ) { info->info.serverInfo.pchSenderAddress = xppMalloc( 256 ); } else { char msg[ 64 ]; sprintf( msg, "TCP error setting up listen sock for port %s", info->info.serverInfo.pchServerPort ); setError( rc, msg ); xppFree( info->info.serverInfo.pchServerPort ); info->info.serverInfo.pchServerPort = NULL; xppFree( info ); info = NULL; ret = SML_ERR_A_XPT_COMMUNICATION; } } *pPrivateServiceInfo = info; return ret;}Ret_t XPTAPI HTTP_deselectProtocol(void *privateServiceInfo) { HttpTransportServiceInfoPtr_t info = ( HttpTransportServiceInfoPtr_t )privateServiceInfo; if ( privateServiceInfo == NULL ) { setError( 1, "privateServiceInfo was NULL" ); return SML_ERR_A_XPT_INVALID_PARM; } if ( info->socketClient ) { if ( info->info.clientInfo.firewallInfo.type != TCP_FIREWALL_DIRECT ) { if ( info->info.clientInfo.firewallInfo.serverName ) { xppFree( info->info.clientInfo.firewallInfo.serverName ); info->info.clientInfo.firewallInfo.serverName = NULL; } } } else { if (info->info.serverInfo.pchServerPort) { deselectServerSocket( info->info.serverInfo.pchServerPort ); xppFree( info->info.serverInfo.pchServerPort ); info->info.serverInfo.pchServerPort = NULL; } if ( info->info.serverInfo.pchSenderAddress ) { xppFree( info->info.serverInfo.pchSenderAddress ); info->info.serverInfo.pchSenderAddress = NULL; } } xppFree( privateServiceInfo ); return SML_ERR_OK;}Ret_t XPTAPI HTTP_openCommunication(void *privateServiceInfo, int role, void **pPrivateConnectionInfo) { Ret_t rc = SML_ERR_OK; HttpTransportConnInfoPtr_t info = NULL; if ( privateServiceInfo == NULL || pPrivateConnectionInfo == NULL ) { setError( 1, "One of privateServiceInfo or privateConnectionInfo pointer was NULL" ); return SML_ERR_A_XPT_INVALID_PARM; } info = (HttpTransportConnInfoPtr_t)xppMalloc( sizeof( HttpTransportConnInfo_t ) ); if (info == NULL) { setError( 2, "Unable to allocate memory for connection Info block" ); return SML_ERR_A_XPT_MEMORY; } info->cbSize = sizeof( HttpTransportConnInfo_t ); info->pServiceInfo = ( HttpTransportServiceInfoPtr_t )privateServiceInfo; info->smlClient = (role == XPT_REQUEST_SENDER); if ( info->pServiceInfo->socketClient ) { rc = tcpOpenConnectionEx(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -