wintransportagent.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 795 行 · 第 1/2 页
CPP
795 行
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*/
/*
How to test SSL connections
----------------------------
On the server:
1) create the keystore:
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA
2) In $CATALINA_HOME/conf/server.xml uncomment the lines:
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8443" minProcessors="5" maxProcessors="75"
enableLookups="true"
acceptCount="10" debug="0" scheme="https" secure="true">
<Factory className="org.apache.catalina.net.SSLServerSocketFactory" clientAuth="false" protocol="TLS"/>
</Connector>
2) Export the certificate from the key store:
%JAVA_HOME%\bin\keytool -export -alias tomcat -file myroot.cer
On the client:
[for _WIN32_WCE]
1) Copy myroot.cer in a device/emulator directory
2) Click on it to import the certificate as a trusted CA
[for WIN32]
1) Connect (via https) to the server using a web-browser (type "https://<server_address>:8443)
2) Accept and install the certificate sent from the server
*/
#include "base/Log.h"
#include "base/messages.h"
#include "base/util/utils.h"
#include "http/constants.h"
#include "http/errors.h"
#include "http/WinTransportAgent.h"
#include "event/FireEvent.h"
#include "base/util/StringBuffer.h"
#ifdef _WIN32_WCE
#include "http/GPRSConnection.h"
#endif
#define ENTERING(func) // LOG.debug("Entering %ls", func);
#define EXITING(func) // LOG.debug("Exiting %ls", func);
#ifdef USE_ZLIB
#include "zlib.h"
#include "base/globalsdef.h"
USE_NAMESPACE
#endif
/**
* Constructor.
* In this implementation newProxy is ignored, since proxy configuration
* is taken from the WinInet subsystem.
*
* @param url the url where messages will be sent with sendMessage()
* @param proxy proxy information or NULL if no proxy should be used
*/
WinTransportAgent::WinTransportAgent(URL& newURL, Proxy& newProxy,
unsigned int maxResponseTimeout,
unsigned int maxmsgsize)
// Use base class constructor to initialize common attributes
: TransportAgent(newURL,
newProxy,
maxResponseTimeout,
maxmsgsize) {
if (maxResponseTimeout == 0) {
setTimeout(DEFAULT_MAX_TIMEOUT);
} else {
setTimeout(maxResponseTimeout);
}
isToDeflate = false;
isFirstMessage = true;
isToInflate = false;
#ifdef _WIN32_WCE
// used by default. check connection before...
if (!EstablishConnection()) {
# ifdef WIN32_PLATFORM_PSPC
//lastErrorCode = ERR_INTERNET_CONNECTION_MISSING;
//sprintf(lastErrorMsg, "%s: %d",
// "Internet Connection Missing",
// ERR_INTERNET_CONNECTION_MISSING);
setErrorF(ERR_INTERNET_CONNECTION_MISSING, "%s: %d",
"Internet Connection Missing",
ERR_INTERNET_CONNECTION_MISSING);
# else
LOG.error("Warning: internet connection missing.");
# endif // #ifdef WIN32_PLATFORM_PSPC
}
#endif // #ifdef _WIN32_WCE
}
WinTransportAgent::~WinTransportAgent(){}
/*
* Sends the given SyncML message to the server specified
* by the install property 'url'. Returns the server's response.
* The response string has to be freed with delete [].
* In case of an error, NULL is returned and lastErrorCode/Msg
* is set.
*/
char* WinTransportAgent::sendMessage(const char* msg) {
LOG.debug("%s", msg);
ENTERING(L"TransportAgent::sendMessage");
#ifdef USE_ZLIB
// This is the locally allocated buffer for the compressed message.
// Must be deleted after send.
Bytef* compr = NULL;
WCHAR* wbuffer = NULL;
WCHAR* buffer = NULL;
#endif
char* bufferA = new char[readBufferSize+1];
int status = -1;
unsigned int contentLength = 0;
WCHAR* wurlHost = NULL;
WCHAR* wurlResource = NULL;
char* p = NULL;
char* response = NULL;
HINTERNET inet = NULL,
connection = NULL,
request = NULL;
// Check sending msg and host.
if (!msg) {
//lastErrorCode = ERR_NETWORK_INIT;
//sprintf(lastErrorMsg, "TransportAgent::sendMessage error: NULL message.");
setError(ERR_NETWORK_INIT, "TransportAgent::sendMessage error: NULL message.");
goto exit;
}
if (!(url.host) || strlen(url.host) == 0) {
//lastErrorCode = ERR_HOST_NOT_FOUND;
//sprintf(lastErrorMsg, "TransportAgent::sendMessage error: %s.", ERRMSG_HOST_NOT_FOUND);
setErrorF(ERR_HOST_NOT_FOUND, "TransportAgent::sendMessage error: %s.", ERRMSG_HOST_NOT_FOUND);
goto exit;
}
DWORD size = 0,
read = 0,
flags = INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_KEEP_CONNECTION | // This is necessary if authentication is required.
INTERNET_FLAG_NO_COOKIES; // This is used to avoid possible server errors on successive sessions.
LPCWSTR acceptTypes[2] = {TEXT("*/*"), NULL};
// Set flags for secure connection (https).
if (url.isSecure()) {
flags = flags
| INTERNET_FLAG_SECURE
| INTERNET_FLAG_IGNORE_CERT_CN_INVALID
| INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
;
}
//
// Open Internet connection.
//
WCHAR* ua = toWideChar(userAgent);
inet = InternetOpen (ua, INTERNET_OPEN_TYPE_PRECONFIG, NULL, 0, 0);
if (ua) {delete [] ua; ua = NULL; }
if (!inet) {
//lastErrorCode = ERR_NETWORK_INIT;
DWORD code = GetLastError();
char* tmp = createHttpErrorMessage(code);
//sprintf (lastErrorMsg, "InternetOpen Error: %d - %s", code, tmp);
setErrorF(ERR_NETWORK_INIT, "InternetOpen Error: %d - %s", code, tmp);
delete [] tmp;
goto exit;
}
LOG.debug("Connecting to %s:%d", url.host, url.port);
//
// Open an HTTP session for a specified site by using lpszServer.
//
wurlHost = toWideChar(url.host);
if (!(connection = InternetConnect (inet,
wurlHost,
url.port,
NULL, // username
NULL, // password
INTERNET_SERVICE_HTTP,
0,
0))) {
//lastErrorCode = ERR_CONNECT;
DWORD code = GetLastError();
char* tmp = createHttpErrorMessage(code);
//sprintf (lastErrorMsg, "InternetConnect Error: %d - %s", code, tmp);
setErrorF(ERR_CONNECT, "InternetConnect Error: %d - %s", code, tmp);
delete [] tmp;
goto exit;
}
LOG.debug("Requesting resource %s", url.resource);
//
// Open an HTTP request handle.
//
wurlResource = toWideChar(url.resource);
if (!(request = HttpOpenRequest (connection,
METHOD_POST,
wurlResource,
HTTP_VERSION,
NULL,
acceptTypes,
flags, 0))) {
//lastErrorCode = ERR_CONNECT;
DWORD code = GetLastError();
char* tmp = createHttpErrorMessage(code);
//sprintf (lastErrorMsg, "HttpOpenRequest Error: %d - %s", code, tmp);
setErrorF(ERR_CONNECT, "HttpOpenRequest Error: %d - %s", code, tmp);
delete [] tmp;
goto exit;
}
//
// Prepares headers
//
WCHAR headers[512];
contentLength = strlen(msg);
// Msg to send is the original msg by default.
// If compression is enabled, it will be switched to
// compr. We don't want to touch this pointer, so
// it's const (msg is also const).
const void* msgToSend = (const void*)msg;
#ifdef USE_ZLIB
if(compression){
// This is the locally allocated buffer for the compressed message.
// Must be deleted after send.
//
// Say the client can accept the zipped content but the first message is clear
//
if (isFirstMessage || !isToDeflate) {
wsprintf(headers, TEXT("Content-Type: %s\r\nContent-Length: %d\r\nAccept-Encoding: deflate"),
SYNCML_CONTENT_TYPE, contentLength);
isFirstMessage = false;
}
else if (isToDeflate) {
//
// DEFLATE (compress data)
//
uLong comprLen = contentLength;
compr = new Bytef[contentLength];
// Compresses the source buffer into the destination buffer.
int err = compress(compr, &comprLen, (Bytef*)msg, contentLength);
if (err != Z_OK) {
//lastErrorCode = ERR_HTTP_DEFLATE;
//sprintf(lastErrorMsg, "ZLIB: error occurred compressing data.");
setError(ERR_HTTP_DEFLATE, "ZLIB: error occurred compressing data.");
delete [] compr;
compr = NULL;
goto exit;
}
// Msg to send is the compressed data.
msgToSend = (const void*)compr;
int uncomprLenght = contentLength;
contentLength = comprLen;
wsprintf(headers, TEXT("Content-Type: %s\r\nContent-Length: %d\r\nAccept-Encoding: deflate\r\nUncompressed-Content-Length: %d\r\nContent-Encoding: deflate"),
SYNCML_CONTENT_TYPE, contentLength, uncomprLenght);
}
}
else {
wsprintf(headers, TEXT("Content-Type: %s\r\nContent-Length: %d"), SYNCML_CONTENT_TYPE, contentLength);
} //end if compression
#else
wsprintf(headers, TEXT("Content-Type: %s\r\nContent-Length: %d"), SYNCML_CONTENT_TYPE, contentLength);
#endif
// Timeout to receive a rensponse from server (default = 5 min).
DWORD timeoutMsec = timeout*1000;
InternetSetOption(request, INTERNET_OPTION_RECEIVE_TIMEOUT, &timeoutMsec, sizeof(DWORD));
//
// Try MAX_RETRIES times to send http request, in case of network errors
//
DWORD errorCode = 0;
int numretries;
for (numretries=0; numretries < MAX_RETRIES; numretries++) {
//
// Send a request to the HTTP server.
//
fireTransportEvent(contentLength, SEND_DATA_BEGIN);
if (!HttpSendRequest(request, headers, wcslen(headers), (LPVOID)msgToSend, contentLength)) {
errorCode = GetLastError();
char* tmp = createHttpErrorMessage(errorCode);
//sprintf(lastErrorMsg, "HttpSendRequest error %d: %s", errorCode, tmp);
setErrorF(GetLastError(), "HttpSendRequest error %d: %s", errorCode, tmp);
LOG.debug("%s", getLastErrorMsg());
if (errorCode == ERROR_INTERNET_OFFLINE_MODE) { // 00002 -> retry
LOG.debug("Offline mode detected: go-online and retry...");
WCHAR* wurl = toWideChar(url.fullURL);
InternetGoOnline(wurl, NULL, NULL);
delete [] wurl;
continue;
}
else if (errorCode == ERROR_INTERNET_TIMEOUT || // 12002 -> out code 2007
errorCode == ERROR_INTERNET_INCORRECT_HANDLE_STATE) { // 12019 -> out code 2007
//lastErrorCode = ERR_HTTP_TIME_OUT;
//sprintf(lastErrorMsg, "Network error: the request has timed out -> exit.");
setError(ERR_HTTP_TIME_OUT, "Network error: the request has timed out -> exit.");
LOG.debug("%s", getLastErrorMsg());
goto exit;
}
else if (errorCode == ERROR_INTERNET_CANNOT_CONNECT) { // 12029 -> out code 2001
//lastErrorCode = ERR_CONNECT;
//sprintf(lastErrorMsg, "Network error: the attempt to connect to the server failed -> exit");
setError(ERR_CONNECT, "Network error: the attempt to connect to the server failed -> exit");
LOG.debug("%s", getLastErrorMsg());
goto exit;
}
// Other network error: retry.
LOG.info("Network error writing data from client: retry %i time...", numretries + 1);
continue;
}
LOG.debug("%s", MESSAGE_SENT);
fireTransportEvent(contentLength, SEND_DATA_END);
//
// Check the status code.
//
size = sizeof(status);
HttpQueryInfo (request,
HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
(LPDWORD)&status,
(LPDWORD)&size,
NULL);
// OK: status 200
if (status == HTTP_STATUS_OK) {
LOG.debug("Data sent succesfully to server. Server responds OK");
break;
}
#if defined(WIN32) && !defined(_WIN32_WCE)
//
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?