home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Magazine / wwwoffle-2.1.tar.gz / wwwoffle-2.1 / http.c < prev    next >
C/C++ Source or Header  |  1998-02-09  |  4KB  |  161 lines

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/http.c 1.10 1998/02/09 20:53:52 amb Exp $
  3.  
  4.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.1.
  5.   Functions for getting URLs using HTTP.
  6.   ******************/ /******************
  7.   Written by Andrew M. Bishop
  8.  
  9.   This file Copyright 1997,98 Andrew M. Bishop
  10.   It may be distributed under the GNU Public License, version 2, or
  11.   any higher version.  See section COPYING of the GNU Public license
  12.   for conditions under which this file may be redistributed.
  13.   ***************************************/
  14.  
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #include <unistd.h>
  20.  
  21. #include "wwwoffle.h"
  22. #include "errors.h"
  23. #include "misc.h"
  24. #include "config.h"
  25. #include "sockets.h"
  26. #include "proto.h"
  27.  
  28. /*+ Set to the name of the proxy if there is one. +*/
  29. static char *proxy=NULL;
  30.  
  31. /*+ The file descriptor of the server. +*/
  32. static int server=-1;
  33.  
  34.  
  35.  
  36. /*++++++++++++++++++++++++++++++++++++++
  37.   Open a connection to get a URL using HTTP.
  38.  
  39.   char *HTTP_Open Returns NULL on success, a useful message on error.
  40.  
  41.   URL *Url The URL to open.
  42.   ++++++++++++++++++++++++++++++++++++++*/
  43.  
  44. char *HTTP_Open(URL *Url)
  45. {
  46.  char *msg=NULL;
  47.  char *colon;
  48.  char *server_host=NULL;
  49.  int server_port=Protocols[Protocol_HTTP].defport;
  50.  
  51.  proxy=WhichProxy(Url->proto,Url->host);
  52.  if(IsLocalNetHost(Url->host))
  53.     proxy=NULL;
  54.  
  55.  if(proxy)
  56.    {
  57.     server_host=(char*)malloc(strlen(proxy)+1);
  58.     strcpy(server_host,proxy);
  59.    }
  60.  else
  61.    {
  62.     server_host=(char*)malloc(strlen(Url->host)+1);
  63.     strcpy(server_host,Url->host);
  64.    }
  65.  
  66.  if((colon=strchr(server_host,':')))
  67.    {
  68.     *colon++=0;
  69.     if(*colon)
  70.        server_port=atoi(colon);
  71.    }
  72.  
  73.  /* Open the connection. */
  74.  
  75.  server=OpenClientSocket(server_host,server_port);
  76.  init_buffer(server);
  77.  
  78.  if(server==-1)
  79.     msg=PrintMessage(Warning,"Cannot open the HTTP connection to %s port %d; [%!s].",server_host,server_port);
  80.  
  81.  free(server_host);
  82.  
  83.  return(msg);
  84. }
  85.  
  86.  
  87. /*++++++++++++++++++++++++++++++++++++++
  88.   Write to the server to request the URL.
  89.  
  90.   char *HTTP_Request Returns NULL on success, a useful message on error.
  91.  
  92.   URL *Url The URL to get.
  93.  
  94.   char *request_head The head of the HTTP request for the URL.
  95.  
  96.   char *request_body The body of the HTTP request for the URL.
  97.   ++++++++++++++++++++++++++++++++++++++*/
  98.  
  99. char *HTTP_Request(URL *Url,char *request_head,char *request_body)
  100. {
  101.  char *msg=NULL;
  102.  char *new_request_head;
  103.  
  104.  if(proxy)
  105.     new_request_head=MakeRequestAuthorised(proxy,request_head);
  106.  else
  107.     new_request_head=MakeRequestNonProxy(request_head);
  108.  
  109.  if(write_string(server,new_request_head)==-1)
  110.     msg=PrintMessage(Warning,"Failed to write to remote HTTP %s [%!s].",proxy?"proxy":"server");
  111.  if(request_body && write_string(server,request_body)==-1)
  112.     msg=PrintMessage(Warning,"Failed to write to remote HTTP %s [%!s].",proxy?"proxy":"server");
  113.  
  114.  if(request_head!=new_request_head)
  115.     free(new_request_head);
  116.  
  117.  return(msg);
  118. }
  119.  
  120.  
  121. /*++++++++++++++++++++++++++++++++++++++
  122.   Read a line from the header of the reply for the URL.
  123.  
  124.   char *HTTP_ReadHead Returns the next line of data, NULL on EOF.
  125.  
  126.   char *line The previous line read.
  127.   ++++++++++++++++++++++++++++++++++++++*/
  128.  
  129. char *HTTP_ReadHead(char *line)
  130. {
  131.  return(read_line_or_timeout(server,line));
  132. }
  133.  
  134.  
  135. /*++++++++++++++++++++++++++++++++++++++
  136.   Read bytes from the body of the reply for the URL.
  137.  
  138.   int HTTP_ReadBody Returns the number of bytes read on success, -1 on error.
  139.  
  140.   char *s A string to fill in with the information.
  141.  
  142.   int n The number of bytes to read.
  143.   ++++++++++++++++++++++++++++++++++++++*/
  144.  
  145. int HTTP_ReadBody(char *s,int n)
  146. {
  147.  return(read_data_or_timeout(server,s,n));
  148. }
  149.  
  150.  
  151. /*++++++++++++++++++++++++++++++++++++++
  152.   Close a connection opened using HTTP.
  153.  
  154.   int HTTP_Close Return 0 on success, -1 on error.
  155.   ++++++++++++++++++++++++++++++++++++++*/
  156.  
  157. int HTTP_Close(void)
  158. {
  159.  return(CloseSocket(server));
  160. }
  161.