home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / wapuniverse-src-0.3.5.build9.tar.gz / wapuniverse-src-0.3.5.build9.tar / wapuniverse-0.3.5.build9 / wsp.c < prev    next >
C/C++ Source or Header  |  2000-11-12  |  6KB  |  203 lines

  1. //---------------------------------------------------------------------------
  2. // wsp.c
  3. // Contains all platform independant WAP/WSP code 
  4. //
  5. // Project: WAPUniverse for PalmOS
  6. // Copyright ⌐ 1999-2000 Filip Onkelinx
  7. //
  8. // http://www.wapuniverse.com/
  9. // filip@onkelinx.com
  10. //
  11. // This program is free software; you can redistribute it and/or
  12. // modify it under the terms of the GNU General Public License
  13. // as published by the Free Software Foundation; either version 2
  14. // of the License, or (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program; if not, write to the Free Software 
  23. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
  24. //
  25. //
  26. // $Workfile: wsp.c $  
  27. // $Author: wapuniverse $  
  28. // $Date: 2000/11/11 20:51:09 $     
  29. // $Revision: 1.6 $
  30. //
  31. // $Header: /cvsroot/wapuniverse/wapuniverse/src/wsp.c,v 1.6 2000/11/11 20:51:09 wapuniverse Exp $
  32. //
  33. //---------------------------------------------------------------------------
  34.  
  35. #include "wsp.h"
  36. #include "WAPUniverse.h"
  37.  
  38. static char *wspStatusCodeStr[] = { 
  39.         "Multiple choices", // 0x30
  40.         "Moved permanently", // 0x31
  41.         "Moved temporarily", // 0x32
  42.         "See other", // 0x33
  43.         "Not modified", // 0x34
  44.         "Use proxy", // 0x35
  45.         "-", // 0x36
  46.         "-", // 0x37
  47.         "-", // 0x38
  48.         "-", // 0x39
  49.         "-", // 0x3A
  50.         "-", // 0x3B
  51.         "-", // 0x3C
  52.         "-", // 0x3D
  53.         "-", // 0x3E
  54.         "-", // 0x3F
  55.         "Bad request", // 0x40
  56.         "Unauthorized", // 0x41
  57.         "Payment required", // 0x42
  58.         "Forbidden", // 0x43
  59.         "Not found", //0x44
  60.         "Method not Allowed", //0x45
  61.         "Not acceptable", //0x46
  62.         "Proxy auth. required", //0x47
  63.         "Request timeout", //0x48
  64.         "Conflict", //0x49
  65.         "Gone", //0x4A
  66.         "Length required", //0x4B
  67.         "Precondition failed", //0x4C
  68.         "Request entity too large", //0x4D
  69.         "Request-URI too large", // 0X4E
  70.         "Unsupported media type", // 0x4F
  71.         "-", // 0x50
  72.         "-", // 0x51
  73.         "-", // 0x52
  74.         "-", // 0x53
  75.         "-", // 0x54
  76.         "-", // 0x55
  77.         "-", // 0x56
  78.         "-", // 0x37
  79.         "-", // 0x58
  80.         "-", // 0x59
  81.         "-", // 0x5A
  82.         "-", // 0x5B
  83.         "-", // 0x5C
  84.         "-", // 0x5D
  85.         "-", // 0x5E
  86.         "-", // 0x5F
  87.         "internal server error", // 0x60
  88.         "Not implemented", // 0x61
  89.         "Bad Gateway", // 0x62
  90.         "Service unavailable", // 0x63
  91.         "Gateway Timeout", // 0x64
  92.         "HTTP version not supported" // 0x65
  93. };  
  94.  
  95. char *WspHostErrStr()
  96. {
  97.     static char msgstr[32];
  98.  
  99.     if (errno != 0)
  100.         SysErrString(errno, msgstr, 32);
  101.     else
  102.         msgstr[0] = '\0';
  103.  
  104.     return(msgstr);
  105. }
  106.  
  107.  
  108.                                             
  109.  
  110. int WspReadInt(char *instr, int *i)
  111. {
  112.     int v = 0;
  113.     unsigned char c;
  114.     
  115.     do {
  116.         c = instr[(*i)++];
  117.         v = (v << 7)|(c & 0x7f);
  118.     }while (( c & 0x80) !=0);
  119.     
  120.     return (v);
  121.     
  122. }
  123.  
  124. int WspDecode (char *instr, int inlen, WSPPDUPtr wsppdu)
  125. {
  126.  int j,i;
  127.  int sz;
  128.  
  129.   i=0;
  130.   wsppdu->data = NULL;
  131.   wsppdu->transaction_id = instr[i++];
  132.   wsppdu->pdu_type = instr[i++];
  133.   switch(wsppdu->pdu_type) {
  134.     case  WSP_PDU_REPLY : // reply
  135.           wsppdu->status_code = instr[i++];
  136.         if (wsppdu->status_code != WSP_STATUS_OK) {
  137.             if ((wsppdu->status_code >= 0x30) && (wsppdu->status_code <= 0x65)) {
  138.                 MyErrorFunc("REPLY STATUS NOT OK:",wspStatusCodeStr[(wsppdu->status_code)-0x30]);
  139.             }else{
  140.                 MyErrorFunc("REPLY STATUS NOT OK:","unkown");
  141.             }
  142.             return(-wsppdu->status_code);
  143.         }
  144.             // get the content type and header size, skip for the moment
  145.           sz = WspReadInt(instr,&i);  
  146.           i+=sz;  //TODO: don't skip but do something with it
  147.           // Data
  148.           sz = inlen-i;
  149.         wsppdu->data=Malloc(sz+1); 
  150.            ErrFatalDisplayIf (!wsppdu->data, "WspDecode: Malloc Failed");
  151.           for (j=0;j<(sz);j++)
  152.                 wsppdu->data[j] = instr[i++];
  153.           wsppdu->length=sz;
  154.         break;
  155.     default:
  156.             DisplayError("Unhandled PDUTYPE !!");
  157.         break;
  158.   }
  159.   return(0);
  160. }
  161.  
  162. int WspFreePdu(WSPPDUPtr wsppdu)
  163. {
  164.     if (wsppdu->data != NULL)
  165.         Free(wsppdu->data);
  166.     wsppdu->data = NULL;
  167.     return(0);
  168. }
  169.  
  170. Int16 WspGetReply(Int16 sock, char *buf, Int16 buflen, UInt32 maxTimer,GlobalsType *g)
  171. {
  172.     Int16     ret; 
  173.  
  174.     // please don't turn me off now
  175.     EvtResetAutoOffTimer();     
  176.     ret = recv(sock,buf,buflen,0);
  177.     if ((errno==netErrTimeout) && ( TimGetTicks() < maxTimer ) ) {
  178.         return(0);
  179.     }
  180.     if (ret <= 0) {
  181.         g->state = BS_IDLE;
  182.         FrmSetTitle(FrmGetActiveForm(),"WAPUniverse");
  183.         if (ret == 0) {
  184.             DisplayError("recv failed/Closed by remote");        
  185.             ret = -1;
  186.         }
  187.         MyErrorFunc("getReply ",WspHostErrStr());
  188.         if (errno == ENOMEM) {
  189.             DisplayError("ENOMEM");        
  190.         }
  191.     } else {
  192.         g->state = BS_WSP_DECODE;
  193.     }
  194.     // close socket if needed              
  195.     if (sock >= 0)
  196.         close (sock);
  197.     sock = -1;
  198.     return(ret);
  199. }
  200.  
  201.  
  202.  
  203.