home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / internet misc / GetTLE 1.0 / GetTLE.exe / Src / http.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-26  |  6.9 KB  |  249 lines

  1. /*
  2.     GetTLE - http.c - Handling of the HTTP connection
  3.     Copyright ⌐2000 Andreas Schneider
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. //    Thanks to Neil Rhodes and Julie McKeehan
  21. //    for their sendmail example in
  22. //    'Palm Programming: the Developer's Guide'
  23. //  It saved me hours of work
  24. //  Andreas
  25.  
  26. #ifdef linux
  27. // includes and defines to compile with Linux
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>   
  33. #include <netdb.h>
  34. #include <errno.h>
  35. #include "tcpip.h"
  36. #include "alerts.h"
  37. #include "debug.h"
  38. #include "http.h" // Stuff for this file
  39. // no need for the progress indicator
  40. #define DisplayProgress(b,f,x,y)
  41. // again - just use printf
  42. #define PSatDBAppendLine(s) printf("TLE: %s\n",s)
  43. #else
  44. // The includes for PalmOS
  45. #include <PalmOs.h>
  46. #include <sys_socket.h> // Use Berkeley sockets headers, not NetLib
  47. #include "http.h" // Stuff for this file
  48. #include "alerts.h"
  49. #include "PSatDB.h" // For access to the PocketSat database
  50. #include "debug.h" // for easy debugging
  51. #include "Progress.h" // for the progress indicator
  52. #include "tcpip.h"
  53. #endif
  54.  
  55. static char CRLF[] ="\r\n";
  56.  
  57. // Longer lines will most likely not contain TLE data anyway
  58. #define MAX_LINE_SIZE 200
  59.  
  60. char socket_buf[10000]="";
  61.  
  62. // reads and handles lines until we run out of them
  63. // It recognizes TLE lines and returns the number of
  64. // TLE sets found
  65. static int ReadReply(Socket sock)
  66. {
  67.     int    num_bytes; // read in one go
  68.     unsigned long total_bytes=0; // read overall
  69.     char lines[3][MAX_LINE_SIZE]={{0},{0},{0}}; // TLE 
  70.     unsigned int lines_read=0;
  71.     unsigned int TLEs_read=0;
  72.     unsigned int current_line=0;
  73.     unsigned int prev_line=1;
  74.     unsigned int prev_prev_line=2;
  75.  
  76.     do
  77.     {
  78.       // read a line in one of the three buffers
  79.         num_bytes = SocketReadLine(&sock,lines[current_line],MAX_LINE_SIZE);
  80.         if (num_bytes>0)
  81.         {
  82.           // LogMessage("%s\n",lines[current_line]);
  83.           // here comes more data
  84.           // keep track of the total amount received in the document
  85.           lines_read++;
  86.           total_bytes+=num_bytes;
  87.           // try to figure out if have TLE lines now
  88.           // I want 3 lines minimum
  89.           if (lines_read>=3)
  90.           {
  91.             // The previous line is _not_ (currentLine-1)%3 because
  92.             // currentLine is unsigned. So the previous line is of course:
  93.             prev_line=(current_line+2)%3;
  94.             // and the line before that is
  95.             prev_prev_line=(current_line+1)%3;
  96.             // How do we recognize a TLE? So let's look at the definition of
  97.             // them according to the FAQ at www.celes.com
  98.             // They consist of two 69 -character lines
  99.             if (strlen(lines[current_line])==69 &&
  100.                 strlen(lines[prev_line])==69)
  101.             {
  102.               // So there are the two 69-character lines
  103.               // let's check one more thing: The first character of
  104.               // each line must be the line number
  105.               if (lines[current_line][0]=='2'  &&
  106.                   lines[prev_line][0]=='1')
  107.               {
  108.                 // now that is good enough for me - I assume its a TLE set
  109.                 // and that the previous line was the satellite name
  110.                 PSatDBAppendLine(lines[prev_prev_line]);
  111.                 PSatDBAppendLine(lines[prev_line]);
  112.                 PSatDBAppendLine(lines[current_line]);
  113.                 TLEs_read++;
  114.                 // maybe add some progress indicator here?
  115.               }
  116.             }
  117.           }
  118.           // false == don't erase before displaying
  119.           DisplayProgress(false,"Got %lu bytes %u TLEs",total_bytes,TLEs_read);
  120.         }
  121.         else if (num_bytes==0)
  122.         {
  123.           // just ignore empty lines
  124.         }
  125.         else if (num_bytes==-1)
  126.         {
  127.           LogMessage("Connection closed.\n");
  128.         }
  129.         else
  130.         {
  131.           LogMessage("Read error: #%i\n",num_bytes);
  132.         }
  133.         // next line
  134.         current_line++;
  135.         current_line %=3;
  136.     } while (num_bytes >= 0);
  137.     LogMessage("%i bytes read\n",total_bytes);
  138.     return TLEs_read;
  139. }
  140.  
  141.  
  142.  
  143. // sends s1 followed by s2 followed by s3 followed by CRLF over socket sock
  144. // any of the strings can be NULL to be ignored
  145. static int SendLine(Socket sock, char *s1, char *s2, char *s3)
  146. {
  147.   int success=0;
  148.     
  149.     if (s1 && SocketWrite(&sock, s1, strlen(s1)) < 0)
  150.     {
  151.         MyErrorFunc("Write error",NULL);
  152.     }
  153.     else if (s2 && SocketWrite(&sock, s2, strlen(s2)) < 0)
  154.     {
  155.         MyErrorFunc("Write error",NULL);
  156.     }
  157.     else if (s3 && SocketWrite(&sock, s3, strlen(s3)) < 0)
  158.     {
  159.         MyErrorFunc("Write error",NULL);
  160.     }
  161.     else if (SocketWrite(&sock, CRLF, strlen(CRLF)) < 0)
  162.     {
  163.       MyErrorFunc("Write error",NULL);
  164.     }
  165.     else
  166.     {
  167.       success=1;
  168.     }
  169.     return success;
  170. }
  171.  
  172. // Request a document that might contain TLEs
  173. int RequestDocument(char *URL)
  174. {
  175.     int TLEs = 0;
  176.     Socket sock = {0}; // socket file descriptor
  177.     char hostname[128]="";
  178.     char filename[128]="";
  179.     char *URL_ptr;
  180.     char *hostname_ptr="";
  181.     char *filename_ptr;
  182.     
  183.   LogMessage("Requesting http://%s\n",URL);
  184.   // first we split the URL into a hostname and a filename
  185.   // copy into hostname until we find the first forward slash
  186.   for (hostname_ptr=hostname,URL_ptr=URL;*URL_ptr && *URL_ptr!='/';URL_ptr++,hostname_ptr++)
  187.   {
  188.     *hostname_ptr=*URL_ptr;
  189.   }
  190.   // then terminate with \0
  191.   *hostname_ptr='\0';
  192.   // copy everything else into filename
  193.   for (filename_ptr=filename;*URL_ptr;URL_ptr++,filename_ptr++)
  194.   {
  195.     *filename_ptr=*URL_ptr;
  196.   }
  197.   // and terminate with \0
  198.   *filename_ptr='\0';
  199.   LogMessage("Host: %s\n",hostname);
  200.   LogMessage("File:%s\n",filename);
  201.   TcpipSetStatusCallback(MyStatusFunc);
  202.   TcpipSetErrorCallback(MyErrorFunc);
  203.   TcpipSetDebugCallback(VLogMessage);
  204.     // open connection to the server
  205.     sock.socket=-1;
  206.     //sock = MakeConnection("http","tcp",hostname,socket_buf,sizeof(socket_buf));
  207.     sock = MakeConnection("http","tcp",hostname,NULL,0);
  208.     if (sock.socket<0)
  209.     {
  210.         MyErrorFunc("Couldn't open connection", NULL);
  211.     }
  212.     else
  213.     {
  214.       // send & receive the data
  215.       LogMessage("Sending request\n");        
  216.     if (SendLine(sock, "GET ", filename, " HTTP/1.0"))
  217.     {
  218.       LogMessage("Request sent.\n");
  219.         // http requires us to send an extra CRLF terminated 
  220.         // empty line to finish the request
  221.         if (SendLine(sock,NULL,NULL,NULL))
  222.         {
  223.           // Now we'll try to read the reply
  224.           TLEs=ReadReply(sock);
  225.                 //dump_socket(sock); // uncomment to just read
  226.         }
  227.       }
  228.     }
  229.     // cleanup
  230.     SocketClose(&sock);
  231.     return TLEs;
  232. }
  233.  
  234. #ifdef linux
  235. // For Linux we'll also supply a main function for easy testing
  236.  
  237. int main(int argc, char **argv)
  238. {
  239.   char *URL="localhost/index.html";
  240.  
  241.     if (argc>1)
  242.     {
  243.       URL=argv[1];
  244.     }
  245.   RequestDocument(URL);
  246.     return 0;
  247. }
  248.  
  249. #endif