home *** CD-ROM | disk | FTP | other *** search
- /*
- GetTLE - http.c - Handling of the HTTP connection
- Copyright ⌐2000 Andreas Schneider
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- 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 General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- // Thanks to Neil Rhodes and Julie McKeehan
- // for their sendmail example in
- // 'Palm Programming: the Developer's Guide'
- // It saved me hours of work
- // Andreas
-
- #ifdef linux
- // includes and defines to compile with Linux
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <errno.h>
- #include "tcpip.h"
- #include "alerts.h"
- #include "debug.h"
- #include "http.h" // Stuff for this file
- // no need for the progress indicator
- #define DisplayProgress(b,f,x,y)
- // again - just use printf
- #define PSatDBAppendLine(s) printf("TLE: %s\n",s)
- #else
- // The includes for PalmOS
- #include <PalmOs.h>
- #include <sys_socket.h> // Use Berkeley sockets headers, not NetLib
- #include "http.h" // Stuff for this file
- #include "alerts.h"
- #include "PSatDB.h" // For access to the PocketSat database
- #include "debug.h" // for easy debugging
- #include "Progress.h" // for the progress indicator
- #include "tcpip.h"
- #endif
-
- static char CRLF[] ="\r\n";
-
- // Longer lines will most likely not contain TLE data anyway
- #define MAX_LINE_SIZE 200
-
- char socket_buf[10000]="";
-
- // reads and handles lines until we run out of them
- // It recognizes TLE lines and returns the number of
- // TLE sets found
- static int ReadReply(Socket sock)
- {
- int num_bytes; // read in one go
- unsigned long total_bytes=0; // read overall
- char lines[3][MAX_LINE_SIZE]={{0},{0},{0}}; // TLE
- unsigned int lines_read=0;
- unsigned int TLEs_read=0;
- unsigned int current_line=0;
- unsigned int prev_line=1;
- unsigned int prev_prev_line=2;
-
- do
- {
- // read a line in one of the three buffers
- num_bytes = SocketReadLine(&sock,lines[current_line],MAX_LINE_SIZE);
- if (num_bytes>0)
- {
- // LogMessage("%s\n",lines[current_line]);
- // here comes more data
- // keep track of the total amount received in the document
- lines_read++;
- total_bytes+=num_bytes;
- // try to figure out if have TLE lines now
- // I want 3 lines minimum
- if (lines_read>=3)
- {
- // The previous line is _not_ (currentLine-1)%3 because
- // currentLine is unsigned. So the previous line is of course:
- prev_line=(current_line+2)%3;
- // and the line before that is
- prev_prev_line=(current_line+1)%3;
- // How do we recognize a TLE? So let's look at the definition of
- // them according to the FAQ at www.celes.com
- // They consist of two 69 -character lines
- if (strlen(lines[current_line])==69 &&
- strlen(lines[prev_line])==69)
- {
- // So there are the two 69-character lines
- // let's check one more thing: The first character of
- // each line must be the line number
- if (lines[current_line][0]=='2' &&
- lines[prev_line][0]=='1')
- {
- // now that is good enough for me - I assume its a TLE set
- // and that the previous line was the satellite name
- PSatDBAppendLine(lines[prev_prev_line]);
- PSatDBAppendLine(lines[prev_line]);
- PSatDBAppendLine(lines[current_line]);
- TLEs_read++;
- // maybe add some progress indicator here?
- }
- }
- }
- // false == don't erase before displaying
- DisplayProgress(false,"Got %lu bytes %u TLEs",total_bytes,TLEs_read);
- }
- else if (num_bytes==0)
- {
- // just ignore empty lines
- }
- else if (num_bytes==-1)
- {
- LogMessage("Connection closed.\n");
- }
- else
- {
- LogMessage("Read error: #%i\n",num_bytes);
- }
- // next line
- current_line++;
- current_line %=3;
- } while (num_bytes >= 0);
- LogMessage("%i bytes read\n",total_bytes);
- return TLEs_read;
- }
-
-
-
- // sends s1 followed by s2 followed by s3 followed by CRLF over socket sock
- // any of the strings can be NULL to be ignored
- static int SendLine(Socket sock, char *s1, char *s2, char *s3)
- {
- int success=0;
-
- if (s1 && SocketWrite(&sock, s1, strlen(s1)) < 0)
- {
- MyErrorFunc("Write error",NULL);
- }
- else if (s2 && SocketWrite(&sock, s2, strlen(s2)) < 0)
- {
- MyErrorFunc("Write error",NULL);
- }
- else if (s3 && SocketWrite(&sock, s3, strlen(s3)) < 0)
- {
- MyErrorFunc("Write error",NULL);
- }
- else if (SocketWrite(&sock, CRLF, strlen(CRLF)) < 0)
- {
- MyErrorFunc("Write error",NULL);
- }
- else
- {
- success=1;
- }
- return success;
- }
-
- // Request a document that might contain TLEs
- int RequestDocument(char *URL)
- {
- int TLEs = 0;
- Socket sock = {0}; // socket file descriptor
- char hostname[128]="";
- char filename[128]="";
- char *URL_ptr;
- char *hostname_ptr="";
- char *filename_ptr;
-
- LogMessage("Requesting http://%s\n",URL);
- // first we split the URL into a hostname and a filename
- // copy into hostname until we find the first forward slash
- for (hostname_ptr=hostname,URL_ptr=URL;*URL_ptr && *URL_ptr!='/';URL_ptr++,hostname_ptr++)
- {
- *hostname_ptr=*URL_ptr;
- }
- // then terminate with \0
- *hostname_ptr='\0';
- // copy everything else into filename
- for (filename_ptr=filename;*URL_ptr;URL_ptr++,filename_ptr++)
- {
- *filename_ptr=*URL_ptr;
- }
- // and terminate with \0
- *filename_ptr='\0';
- LogMessage("Host: %s\n",hostname);
- LogMessage("File:%s\n",filename);
- TcpipSetStatusCallback(MyStatusFunc);
- TcpipSetErrorCallback(MyErrorFunc);
- TcpipSetDebugCallback(VLogMessage);
- // open connection to the server
- sock.socket=-1;
- //sock = MakeConnection("http","tcp",hostname,socket_buf,sizeof(socket_buf));
- sock = MakeConnection("http","tcp",hostname,NULL,0);
- if (sock.socket<0)
- {
- MyErrorFunc("Couldn't open connection", NULL);
- }
- else
- {
- // send & receive the data
- LogMessage("Sending request\n");
- if (SendLine(sock, "GET ", filename, " HTTP/1.0"))
- {
- LogMessage("Request sent.\n");
- // http requires us to send an extra CRLF terminated
- // empty line to finish the request
- if (SendLine(sock,NULL,NULL,NULL))
- {
- // Now we'll try to read the reply
- TLEs=ReadReply(sock);
- //dump_socket(sock); // uncomment to just read
- }
- }
- }
- // cleanup
- SocketClose(&sock);
- return TLEs;
- }
-
- #ifdef linux
- // For Linux we'll also supply a main function for easy testing
-
- int main(int argc, char **argv)
- {
- char *URL="localhost/index.html";
-
- if (argc>1)
- {
- URL=argv[1];
- }
- RequestDocument(URL);
- return 0;
- }
-
- #endif