📄 xpt-http.c
字号:
/*************************************************************************//* module: Communication Services, HTTP functions *//* file: src/xpt/all/xpt-http.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 <xpttypes.h>#include "xptihttp.h"#include "xpt-tcp.h"#include "xpt-http.h"#include "xpt-auth.h"#include <xpt.h>#include <xptport.h>#define CACHE_BUFFER_SIZE 2000 // Size of the transfer buffer#define CHUNK_HEADER_SIZE 8 // size of a chunk header. // must be greater than (CACHE_BUFFER_SIZE LOG 10) + 2#define HTTP_STATUS_UNDEFINED -1#define HTTP_DEFAULT_CONNECTION_TYPE CLOSE // KEEP_ALIVE// %%% luz: added ifdefs to allow pre-defining these values in the prefix file// - HTTP language#ifndef HTTP_LANGUAGE #define HTTP_LANGUAGE "en"#endif// - server name#ifndef SERVER_NAME #define SERVER_NAME "HTTP SyncML Server/0.50.0.0"#endif// - client name#ifndef CLIENT_NAME #if defined(__EPOC_OS__) #define CLIENT_NAME "HTTP SyncML Client ["HTTP_LANGUAGE"] (EPOC; I)" #elif defined(__PALM_OS__) #define CLIENT_NAME "HTTP SyncML Client ["HTTP_LANGUAGE"] (PalmOS; I)" #elif defined(WINCE) #define CLIENT_NAME "HTTP SyncML Client ["HTTP_LANGUAGE"] (WinCE; I)" #elif defined(WIN32) #define CLIENT_NAME "HTTP SyncML Client ["HTTP_LANGUAGE"] (Win32; I)" #elif defined(linux) #define CLIENT_NAME "HTTP SyncML Client ["HTTP_LANGUAGE"] (Linux; I)" #endif#endif// - other HTTP headers#define HTTP_VERSION "HTTP/1.1"#define HTTP_MIME_TYPES "application/vnd.syncml+xml, application/vnd.syncml+wbxml, */*"#define HTTP_CACHECONTROL "private"#ifndef min #define min(a, b) ((a) < (b) ? (a) : (b))#endif/*************************************//* Type definition for object store *//*************************************/typedef struct{ XptHmacInfo_t hmacInfo; char *pchHmac;} HttpHmacInfo_t, *HttpHmacInfoPtr_t;typedef struct { BufferSize_t cbSize; // size of this data structure char *pchRequest; // HTTP request: "RECEIVE", "SEND", "EXCHANGE" or "SERVER" char *pchURI; // URL name char *pchHost; // Host IP address char *pchProxy; // Proxy IP address char *pchResponseType; // MIME type of data received char *pchRequestType; // MIME type of data xfered char *pchReferer; // Referenced URL char *pchFrom; // address of sender HttpHmacInfo_t sXSyncmlHmac; // HMAC Information BufferSize_t cbDataLength; // Length of real data Bool_t bEox; // end-of-transmission flag enum { // HTTP requests supported MODE_HTTP_SERVER = 0, MODE_HTTP_GET = 1, MODE_HTTP_PUT = 2, MODE_HTTP_POST = 3 } fOpenMode; // Communication open mode enum { MODE_UNDEFINED = 0, MODE_READING = 1, MODE_WRITING = 2 } fTransferMode; // Transfer mode enum { NOT_CHUNKED = 0, CHUNKED = 1 } fTransferCoding; // data transfer coding enum { CLOSE = 0, KEEP_ALIVE = 1 } fConnection; // connection type // enum // { // FLAT = 0, // BASE64 = 1 // } fEncoding; // data encoding type TcpRc_t iRc; // Communication return code int iHttpStatus; // status of HTTP response SocketPtr_t pSession; // TCP/IP socket // unsigned char abBase64Data [4]; // needed by base64 encoding routine // BufferSize_t cbDocumentOffset; // total # of document bytes read. BufferSize_t cbDataToRead; // # of bytes already read. BufferSize_t cbCacheSize; // size of data xfer buffer unsigned char pbCache [CACHE_BUFFER_SIZE]; // Data xfer buffer BufferSize_t cbCacheUsed; // number of bytes processed #ifdef STATIC_MEMORY_MANAGEMENT BufferSize_t cbStringHeapSize; // Size of string heap buffer BufferSize_t cbStringHeapUsed; // pointer to free string heap space char pchStringBuffer [STATIC_MEMORY_SIZE]; // memory buffer for strings #endif } HttpBuffer_t, *HttpBufferPtr_t;/* the following enumeration denotes the data encoding type *//*****************************************************************************//* *//* String Management functions *//* *//*****************************************************************************/#ifdef STATIC_MEMORY_MANAGEMENT#define makeString(s) newString ((s),p)// %%%luz 2002-08-28: second argument p was missing here:#define makeHmacString(s,p) newHmacString((s),(p))#define freeString(s)/*************************************************//* Function: copy a string into the string heap, *//* return a pointer to the copied string *//*************************************************/StringBuffer_t newString (CString_t pszString, HttpBufferPtr_t p) { StringBuffer_t rc = p->pchStringBuffer + p->cbStringHeapUsed; if (pszString != NULL) { StringLength_t cbStringLength = xppStrlen (pszString); if (p->cbStringHeapSize < cbStringLength + 1 + p->cbStringHeapUsed) rc = NULL; else { xppStrcpy (p->pchStringBuffer + p->cbStringHeapUsed, pszString); p->cbStringHeapUsed += cbStringLength + 1; } } else rc = NULL; return rc; }// %%%luz 2002-08-28: second argument p was missing hereStringBuffer_t newHmacString(XptHmacInfoPtr_t pHmacInfo, HttpBufferPtr_t p){ StringBuffer_t rc = p->pchStringBuffer + p->cbStringHeapUsed; StringLength_t cbStringLength; if ((pHmacInfo != NULL) && (pHmacInfo->username != NULL) && (pHmacInfo->mac != NULL)) { cbStringLength = xppStrlen("algorithm=") + xppStrlen( (pHmacInfo->algorithm != NULL) ? "MD5" : pHmacInfo->algorithm) + xppStrlen(", username=") + xppStrlen (pHmacInfo->username) + xppStrlen(", mac=") + xppStrlen (pHmacInfo->mac); if (p->cbStringHeapSize < cbStringLength + 1 + p->cbStringHeapUsed) rc = NULL; else { sprintf(p->pchStringBuffer + p->cbStringHeapUsed, "algorithm=%s, username=%s, mac=%s", (pHmacInfo->algorithm == NULL) ? "MD5" : pHmacInfo->algorithm, pHmacInfo->username, pHmacInfo->mac); p->cbStringHeapUsed += cbStringLength + 1; } } else rc = NULL; return rc; }#elsestatic char *copyString(const char *string) { char *copy = xppMalloc(xppStrlen(string) + 1); if (!copy) return copy; /* Unable to allocate storage */ xppStrcpy(copy, string); return copy;}StringBuffer_t copyHmacString(XptHmacInfoPtr_t pHmacInfo){ StringLength_t cbStringLength; char *copy; if ((pHmacInfo != NULL) && (pHmacInfo->username != NULL) && (pHmacInfo->mac != NULL)) { cbStringLength = xppStrlen("algorithm=") + xppStrlen( (pHmacInfo->algorithm == NULL) ? "MD5" : pHmacInfo->algorithm) + xppStrlen(", username=\"") + xppStrlen (pHmacInfo->username) + xppStrlen("\", mac=") + xppStrlen (pHmacInfo->mac); copy = xppMalloc(cbStringLength + 1); if (!copy) return copy; /* Unable to allocate storage */ sprintf(copy, "algorithm=%s, username=\"%s\", mac=%s", (pHmacInfo->algorithm == NULL) ? "MD5" : pHmacInfo->algorithm, pHmacInfo->username, pHmacInfo->mac); } else copy = NULL; return copy;}/* No static memory management: use CRT memory heap */#define makeString(s) ((s) ? copyString(s) : NULL)// %%%luz 2002-08-28: second argument p was missing here (not really// used in non-static memory management case):#define makeHmacString(s,p) copyHmacString((s))#define freeString(s) if ((s)) xppFree ((s))#endif/*****************************************************************************//*****************************************************************************//** **//** private functions **//** **//*****************************************************************************//*****************************************************************************//* check if the handle that has been passed by the caller is correct. */// Note: gcc cannot parse multi-line macros in files with DOS lineends#define ISHEADERVALID(p) (((p) != NULL) && ((p)->cbSize == sizeof (HttpBuffer_t)))/* compute the logarithm of an integer */#define LOG(n) ((n)>10000?5:((n)>1000?4:((n)>100?3:((n)>10?2:1))))/*********************************************//* Function: remove the "HTTP://<hostname>/" *//* stuff from the specified URI *//* Returns: the resource name *//*********************************************/const char *getResourceName (const char *pszURI) { const char *rc = NULL; if ((xppStrlen (pszURI) > 7) && (pszURI [0] == 'H' || pszURI [0] == 'h') && (pszURI [1] == 'T' || pszURI [1] == 't') && (pszURI [2] == 'T' || pszURI [2] == 't') && (pszURI [3] == 'P' || pszURI [3] == 'p') && pszURI [4] == ':' && pszURI [5] == '/' && pszURI [6] == '/') rc = xppStrchr (pszURI+7, '/'); if (rc == NULL) rc = pszURI; else rc ++; return rc; }//Bool_t isBinaryMimeType (CString_t pszName)// {// static struct { CString_t name; Bool_t type; } mimetypes [] =// { { "application/vnd.syncml-xml", false },// { "application/vnd.syncml-wbxml", false },// { "text/html", false },// { "text/plain", false },// { NULL, false } };// int n;//// for (n = 0; mimetypes [n].name != NULL; n ++)// {// if (!xppStricmp (pszName, mimetypes [n].name))// break;// }// return mimetypes[n].type;// }void writeGeneralRequestHeader (HttpBufferPtr_t p, char *pszHeader) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -