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

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/finger.c 1.1 1998/02/10 19:17:49 amb Exp $
  3.  
  4.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.1.
  5.   Functions for getting URLs using Finger.
  6.   ******************/ /******************
  7.   Written by Andrew M. Bishop
  8.  
  9.   This file Copyright 1998 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. /*+ A buffer to contain the reply head. +*/
  35. static char bufferhead[]="HTTP/1.0 200 OK\r\n"
  36.                          "Content-Type: text/plain\r\n"
  37.                          "\r\n";
  38.  
  39. /*+ The number of characters in the buffer +*/
  40. static int nbufferhead=sizeof(bufferhead), /*+ in total for the head . +*/
  41.            nreadhead=0;         /*+ that have been read from the head. +*/
  42.  
  43.  
  44. /*++++++++++++++++++++++++++++++++++++++
  45.   Open a connection to get a URL using Finger.
  46.  
  47.   char *Finger_Open Returns NULL on success, a useful message on error.
  48.  
  49.   URL *Url The URL to open.
  50.   ++++++++++++++++++++++++++++++++++++++*/
  51.  
  52. char *Finger_Open(URL *Url)
  53. {
  54.  char *msg=NULL;
  55.  char *colon;
  56.  char *server_host=NULL;
  57.  int server_port=Protocols[Protocol_Finger].defport;
  58.  
  59.  proxy=WhichProxy(Url->proto,Url->host);
  60.  if(IsLocalNetHost(Url->host))
  61.     proxy=NULL;
  62.  
  63.  if(proxy)
  64.    {
  65.     server_host=(char*)malloc(strlen(proxy)+1);
  66.     strcpy(server_host,proxy);
  67.    }
  68.  else
  69.    {
  70.     server_host=(char*)malloc(strlen(Url->host)+1);
  71.     strcpy(server_host,Url->host);
  72.    }
  73.  
  74.  if((colon=strchr(server_host,':')))
  75.    {
  76.     *colon++=0;
  77.     if(*colon)
  78.        server_port=atoi(colon);
  79.    }
  80.  
  81.  /* Open the connection. */
  82.  
  83.  server=OpenClientSocket(server_host,server_port);
  84.  init_buffer(server);
  85.  
  86.  if(server==-1)
  87.     msg=PrintMessage(Warning,"Cannot open the Finger connection to %s port %d; [%!s].",server_host,server_port);
  88.  
  89.  free(server_host);
  90.  
  91.  return(msg);
  92. }
  93.  
  94.  
  95. /*++++++++++++++++++++++++++++++++++++++
  96.   Write to the server to request the URL.
  97.  
  98.   char *Finger_Request Returns NULL on success, a useful message on error.
  99.  
  100.   URL *Url The URL to get.
  101.  
  102.   char *request_head The head of the Finger request for the URL.
  103.  
  104.   char *request_body The body of the Finger request for the URL.
  105.   ++++++++++++++++++++++++++++++++++++++*/
  106.  
  107. char *Finger_Request(URL *Url,char *request_head,char *request_body)
  108. {
  109.  char *user,*slash;
  110.  char *msg=NULL;
  111.  
  112.  /* Take a simple route if it is proxied. */
  113.  
  114.  if(proxy)
  115.    {
  116.     char *new_request_head=MakeRequestAuthorised(proxy,request_head);
  117.  
  118.     if(write_string(server,new_request_head)==-1)
  119.        msg=PrintMessage(Warning,"Failed to write to remote Finger %s [%!s].",proxy?"proxy":"server");
  120.     if(request_body && write_string(server,request_body)==-1)
  121.        msg=PrintMessage(Warning,"Failed to write to remote Finger %s [%!s].",proxy?"proxy":"server");
  122.  
  123.     if(request_head!=new_request_head)
  124.        free(new_request_head);
  125.  
  126.     return(msg);
  127.    }
  128.  
  129.  /* Else Sort out the path. */
  130.  
  131.  user=(char*)malloc(strlen(Url->path));
  132.  strcpy(user,Url->path+1);
  133.  
  134.  if((slash=strchr(user,'/')))
  135.     *slash=0;
  136.  
  137.  if(*user)
  138.    {
  139.     if(write_formatted(server,"/W %s\r\n",user)==-1)
  140.        msg=PrintMessage(Warning,"Failed to write to remote Finger server [%!s].");
  141.    }
  142.  else
  143.    {
  144.     if(write_string(server,"/W\r\n")==-1)
  145.        msg=PrintMessage(Warning,"Failed to write to remote Finger server [%!s].");
  146.    }
  147.  
  148.  free(user);
  149.  
  150.  nreadhead=0;
  151.  
  152.  return(msg);
  153. }
  154.  
  155.  
  156. /*++++++++++++++++++++++++++++++++++++++
  157.   Read a line from the header of the reply for the URL.
  158.  
  159.   char *Finger_ReadHead Returns the next line of data, NULL on EOF.
  160.  
  161.   char *line The previous line read.
  162.   ++++++++++++++++++++++++++++++++++++++*/
  163.  
  164. char *Finger_ReadHead(char *line)
  165. {
  166.  char *l=line;
  167.  int m;
  168.  
  169.  /* Take a simple route if it is proxied. */
  170.  
  171.  if(proxy)
  172.     return(read_line_or_timeout(server,line));
  173.  
  174.  /* Else send the header. */
  175.  
  176.  if(nreadhead==nbufferhead)
  177.     return(NULL);
  178.  
  179.  for(m=nreadhead;m<nbufferhead;m++)
  180.     if(bufferhead[m]=='\n')
  181.        break;
  182.  
  183.  m++;
  184.  l=(char*)realloc((void*)l,m-nreadhead+1);
  185.  strncpy(l,&bufferhead[nreadhead],m-nreadhead);
  186.  l[m-nreadhead]=0;
  187.  
  188.  nreadhead=m;
  189.  
  190.  return(l);
  191. }
  192.  
  193.  
  194. /*++++++++++++++++++++++++++++++++++++++
  195.   Read bytes from the body of the reply for the URL.
  196.  
  197.   int Finger_ReadBody Returns the number of bytes read on success, -1 on error.
  198.  
  199.   char *s A string to fill in with the information.
  200.  
  201.   int n The number of bytes to read.
  202.   ++++++++++++++++++++++++++++++++++++++*/
  203.  
  204. int Finger_ReadBody(char *s,int n)
  205. {
  206.  return(read_data_or_timeout(server,s,n));
  207. }
  208.  
  209.  
  210. /*++++++++++++++++++++++++++++++++++++++
  211.   Close a connection opened using Finger.
  212.  
  213.   int Finger_Close Return 0 on success, -1 on error.
  214.   ++++++++++++++++++++++++++++++++++++++*/
  215.  
  216. int Finger_Close(void)
  217. {
  218.  return(CloseSocket(server));
  219. }
  220.