⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpt-http.c

📁 SyncML手册及其编程
💻 C
📖 第 1 页 / 共 5 页
字号:
   // Note: gcc cannot parse line continuation in files with DOS lineends (but they are not needed here!)   sprintf (pszHeader + xppStrlen (pszHeader),            "Cache-Control: "HTTP_CACHECONTROL"\r\n"            "Connection: %s\r\n"            "User-Agent: " CLIENT_NAME "\r\n"            "Accept: "HTTP_MIME_TYPES"\r\n"            "Accept-Language: "HTTP_LANGUAGE"\r\n"            "Accept-Charset: utf-8\r\n",            p->fConnection == KEEP_ALIVE ? "keep-alive" : "close");   if ((p->pchReferer != NULL) && (p->pchReferer [0] != '\0'))      sprintf (pszHeader + xppStrlen (pszHeader),               "Referer: %s\r\n", p->pchReferer);   if ((p->pchFrom != NULL) && (p->pchFrom [0] != '\0'))      sprintf (pszHeader + xppStrlen (pszHeader),               "From: %s\r\n", p->pchFrom);   if ((p->pchHost != NULL) && (p->pchHost [0] != '\0'))      sprintf (pszHeader + xppStrlen (pszHeader),               "Host: %s\r\n", p->pchHost);   return;   }void writeContentRequestHeader (HttpBufferPtr_t p, char *pszHeader)   {   if ((p->pchRequestType != NULL) && (p->pchRequestType [0] != '\0'))      sprintf (pszHeader + xppStrlen (pszHeader),               "Content-Type: %s\r\n", p->pchRequestType);   if (p->cbDataLength > 0)      sprintf (pszHeader + xppStrlen (pszHeader),               "Content-Length: %ld\r\n", p->cbDataLength);   /*******************************************************************/   /* if there are data to send, determine the transfer encoding mode */   /*******************************************************************/   if (((p->fOpenMode == MODE_HTTP_POST) || (p->fOpenMode == MODE_HTTP_PUT)) &&       (p->cbDataLength == 0) )      p->fTransferCoding = CHUNKED; // enable chunked transfer coding   else      p->fTransferCoding = NOT_CHUNKED;//    if ((p->pchRequestType == NULL) || !isBinaryMimeType (p->pchRequestType))//       p->fEncoding = FLAT;//    else//       p->fEncoding = BASE64;//   if ((p->fEncoding == BASE64) || (p->fTransferCoding == CHUNKED))   if (p->fTransferCoding == CHUNKED)      {      sprintf (pszHeader+xppStrlen (pszHeader), "Transfer-Encoding: chunked\r\n");////    if (p->fTransferCoding == CHUNKED)//       sprintf (pszHeader + xppStrlen (pszHeader), " chunked");////    if (p->fEncoding == BASE64)//       sprintf (pszHeader + xppStrlen (pszHeader), " base64");////    sprintf (pszHeader + xppStrlen (pszHeader), "\r\n");      }   if ((p->sXSyncmlHmac.pchHmac != NULL) && (p->sXSyncmlHmac.pchHmac[0] != '\0'))	   sprintf (pszHeader + xppStrlen (pszHeader),	            "x-syncml-hmac: %s\r\n", p->sXSyncmlHmac.pchHmac);   return;   }void writeAuthorizationRequestHeader (HttpBufferPtr_t p,                                      char *pszHeader, char *pszMode,                                      HttpAuthenticationPtr_t auth,                                      HttpAuthenticationDest_t dest)   {   HttpAuthenticationType_t fType = authGetType (auth, dest);   const char *pszURI;   const char *pszAuthType = (dest == DEST_PROXY) ?                             "Proxy-Authorization" : "Authorization";   const char *pszDigest = authGetDigest (auth, dest);   if (fType == AUTH_BASIC)      {      sprintf (pszHeader, "%s: Basic %s\r\n",               pszAuthType, pszDigest);      }   else if (fType == AUTH_DIGEST)      {      HttpAuthenticationData_t ad;      HttpAuthenticationUserData_t ud;      ad.cbSize = sizeof (ad);      ud.cbSize = sizeof (ud);      authGetAuthenticationData (auth, dest, &ad);      authGetUserData (auth, dest, &ud);      pszURI = getResourceName (p->pchURI);      sprintf (pszHeader,               "%s: Digest username=\"%s\", realm=\"%s\", "               "nonce=\"%s\", uri=\"%s\", response=\"%s\"",               pszAuthType, ud.szUser, ad.szRealm,               ad.szNonce, pszURI, pszDigest);      if (ad.szQop [0] != '\0')         {         sprintf (pszHeader + xppStrlen (pszHeader),                  ", cnonce=\"%s\", nc=\"%s\", qop=\"%s\"",                  ud.szCNonce, ud.szNC, ad.szQop);         }      if (ad.szOpaque [0] != '\0')         sprintf (pszHeader + xppStrlen (pszHeader), ", opaque=\"%s\"", ad.szOpaque );      sprintf (pszHeader + xppStrlen (pszHeader), "\r\n");      }   return;   }/***************************************************************//* Function: Write a HTTP request header into the buffer cache *//***************************************************************/void writeRequestHeader (HttpBufferPtr_t p, HttpAuthenticationPtr_t auth)   {   char *pszMode = "";   char *pszHeader = (char *) p->pbCache;   char *pfx1 = "http://", *pfx2 = "", *pfx3 = "/";   const char *pszURI = getResourceName(p->pchURI);   if (p->fOpenMode == MODE_HTTP_POST)     pszMode = "POST";   else if (p->fOpenMode == MODE_HTTP_GET) pszMode = "GET";   else if (p->fOpenMode == MODE_HTTP_PUT) pszMode = "PUT";   /*************************************************************/   /* Write the request header line. Use absolute URI addresses */   /* if the connection goes via a proxy.                       */   /*************************************************************/   if (  (p->pchHost == NULL) || (p->pchHost [0] == '\0')       || !xppStrcmp (p->pchURI, "*")       || (p->pchProxy == NULL) || (p->pchProxy [0] == '\0') )      pfx1 = "";   else      pfx2 = p->pchHost;   if ((pszURI [0] == '/') || !xppStrcmp (p->pchURI, "*"))      pfx3 = "";   sprintf (pszHeader,            "%s %s%s%s%s "HTTP_VERSION"\r\n",            pszMode, pfx1, pfx2, pfx3, pszURI);   /*************************************************/   /* Write general header and content header data. */   /*************************************************/   writeGeneralRequestHeader (p, pszHeader);   writeContentRequestHeader (p, pszHeader);   if (authGetType (auth, DEST_SERVER) != AUTH_NONE)      {      authCalcDigest (auth, DEST_SERVER, pszURI,  pszMode);      writeAuthorizationRequestHeader (p, pszHeader+xppStrlen(pszHeader), pszMode, auth, DEST_SERVER);      }   if (authGetType (auth, DEST_PROXY) != AUTH_NONE)      {      authCalcDigest (auth, DEST_PROXY, pszURI, pszMode);      writeAuthorizationRequestHeader (p, pszHeader+xppStrlen(pszHeader), pszMode, auth, DEST_PROXY);      }   sprintf (pszHeader + xppStrlen (pszHeader), "\r\n");   p->cbCacheUsed = xppStrlen (pszHeader);   }/****************************************************************//* Function: Write a HTTP response header into the buffer cache *//****************************************************************/void writeResponseHeader (HttpBufferPtr_t p,                          int rcDocument,                          const HttpReplyBufferPtr_t settings, // i: Response properties                          const HttpAuthenticationPtr_t auth) // i: Authentication info   {   char achTime [50];   StringBuffer_t pchHmac;   _getTime (achTime);  // defined in xptihttp.h   /******************************************/   /* Write the HTTP header,                 */   /* copy the header into the cache,        */   /* but do not send the data.              */   /******************************************/   // Note: gcc cannot parse line continuation in files with DOS lineends (but they are not needed here!)   sprintf ((char *) p->pbCache,            HTTP_VERSION" %d \r\n"            "Server: " SERVER_NAME "\r\n"            "Date: %s\r\n"            "Accept-Ranges: bytes\r\n"            "Cache-Control: "HTTP_CACHECONTROL"\r\n"            "Connection: %s\r\n",            rcDocument, achTime,            p->fConnection == KEEP_ALIVE ? "keep-alive" : "close");   if (settings != NULL)      {      if ((settings->pszTime != NULL) && (settings->pszTime [0] != '\0'))         sprintf ((char *) p->pbCache + xppStrlen ((char *)p->pbCache),                  "Last-Modified: %s\r\n", settings->pszTime);      if ((settings->pszType != NULL) && (settings->pszType [0] != '\0'))         sprintf ((char *) p->pbCache + xppStrlen ((char *)p->pbCache),                  "Content-Type: %s\r\n", settings->pszType);      if (settings->cbLength > 0)         {         p->fTransferCoding = NOT_CHUNKED;         sprintf ((char *) p->pbCache + xppStrlen ((char *)p->pbCache),                  "Content-Length: %ld\r\n", settings->cbLength);         }      else         p->fTransferCoding = CHUNKED;//    if (isBinaryMimeType (settings->pszType))//       p->fEncoding = BASE64;//    else//       p->fEncoding = FLAT;//    if ((p->fTransferCoding == CHUNKED) || (p->fEncoding == BASE64))      if (p->fTransferCoding == CHUNKED)         {         sprintf ((char *)(p->pbCache+xppStrlen ((char *)p->pbCache)), "Transfer-Encoding:");//       if (p->fTransferCoding == CHUNKED)            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)), " chunked");//       if (p->fEncoding == BASE64)//          sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)), " base64");         sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)), "\r\n");         }    // %%%luz 2002-08-28: second argument p was missing here	  pchHmac = makeHmacString(settings->pXSyncmlHmac,p);      if ((pchHmac != NULL) && (pchHmac [0] != '\0'))         sprintf ((char *) p->pbCache + xppStrlen ((char *)p->pbCache),                  "x-syncml-hmac: %s\r\n", pchHmac);	  freeString(pchHmac);      }   /************************************************/   /* Check if the authentication info are present */   /************************************************/   if ((auth != NULL) && ((rcDocument == 401) || (rcDocument == 407))) // authentication required!      {      HttpAuthenticationDest_t dest = (rcDocument == 401) ? DEST_SERVER : DEST_PROXY;      HttpAuthenticationType_t type = authGetType (auth, dest);      if (type != AUTH_NONE)         {         const char *pszDelim = " ";         const char *d2 = ", ";         HttpAuthenticationData_t ad;         ad.cbSize = sizeof (ad);         authGetAuthenticationData (auth, dest, &ad);         sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                  "%s: %s",                  (dest == DEST_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate",                  (type == AUTH_DIGEST) ? "digest" : "basic");         if (ad.szRealm[0] != '\0')            {            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                     "%srealm=\"%s\"", pszDelim, ad.szRealm); pszDelim = d2;            }         if (ad.szNonce[0] != '\0')            {            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                     "%snonce=\"%s\"", pszDelim, ad.szNonce); pszDelim = d2;            }         if (ad.szOpaque[0] != '\0')            {            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                     "%sopaque=\"%s\"", pszDelim, ad.szOpaque); pszDelim = d2;            }         if (ad.szQop[0] != '\0')            {            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                     "%sqop=\"%s\"", pszDelim, ad.szQop); pszDelim = d2;            }         if (ad.szDomain[0] != '\0')            {            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                     "%sdomain=\"%s\"", pszDelim, ad.szDomain); pszDelim = d2;            }         if (ad.bStale != false)            {            sprintf ((char *)(p->pbCache + xppStrlen ((char *)p->pbCache)),                     "%sstale=true", pszDelim); pszDelim = d2;            }         sprintf ((char *) p->pbCache + xppStrlen ((char *)p->pbCache), "\r\n");         }      }   /****************************************************************/   /* Append the terminating empty line to the HTTP request header */   /****************************************************************/   sprintf ((char *) p->pbCache + xppStrlen ((char *)p->pbCache), "\r\n");   p->cbCacheUsed = xppStrlen ((char *)p->pbCache);   if (settings != NULL)      p->fTransferMode = MODE_WRITING;   else      p->fTransferMode = MODE_UNDEFINED;   }/*******************************************************//* Function: parse one token from the specified line   *//* Return the rest of the string                       *//* The function changes the contents of pszLine!!      *//*******************************************************/StringBuffer_t splitToken (StringBuffer_t pszLine, // i: line                           StringPtr_t ppszToken)  // o: ptr to extracted token   {   StringBuffer_t pszToken = pszLine;   StringBuffer_t pszRest = NULL;   /* skip leading blanks */   while (pszToken [0] == ' ') pszToken ++;   pszRest = pszToken;   /* Now we have found the beginning of the token, so let us find the end of it */   while ((pszRest [0] != '\0') && (pszRest [0] != ' ') && (pszRest [0] != ','))      pszRest ++;   if (pszRest [0] != '\0')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -