File: finger.c

package info (click to toggle)
wwwoffle 2.3a-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,452 kB
  • ctags: 1,064
  • sloc: ansic: 10,102; lex: 1,395; sh: 510; makefile: 263; perl: 78
file content (226 lines) | stat: -rw-r--r-- 5,295 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/***************************************
  $Header: /home/amb/wwwoffle/RCS/finger.c 1.2 1998/06/10 20:47:08 amb Exp $

  WWWOFFLE - World Wide Web Offline Explorer - Version 2.2.
  Functions for getting URLs using Finger.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1998 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/


#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include "wwwoffle.h"
#include "errors.h"
#include "misc.h"
#include "config.h"
#include "sockets.h"
#include "proto.h"

/*+ Set to the name of the proxy if there is one. +*/
static char *proxy=NULL;

/*+ The file descriptor of the server. +*/
static int server=-1;

/*+ A buffer to contain the reply head. +*/
static char *bufferhead=NULL;

/*+ The number of characters in the buffer +*/
static int nbufferhead=0,       /*+ in total for the head . +*/
           nreadhead=0;         /*+ that have been read from the head. +*/


/*++++++++++++++++++++++++++++++++++++++
  Open a connection to get a URL using Finger.

  char *Finger_Open Returns NULL on success, a useful message on error.

  URL *Url The URL to open.
  ++++++++++++++++++++++++++++++++++++++*/

char *Finger_Open(URL *Url)
{
 char *msg=NULL;
 char *colon;
 char *server_host=NULL;
 int server_port=Protocols[Protocol_Finger].defport;

 proxy=WhichProxy(Url->proto,Url->host);
 if(IsLocalNetHost(Url->host))
    proxy=NULL;

 if(proxy)
   {
    server_host=(char*)malloc(strlen(proxy)+1);
    strcpy(server_host,proxy);
   }
 else
   {
    server_host=(char*)malloc(strlen(Url->host)+1);
    strcpy(server_host,Url->host);
   }

 if((colon=strchr(server_host,':')))
   {
    *colon++=0;
    if(*colon)
       server_port=atoi(colon);
   }

 /* Open the connection. */

 server=OpenClientSocket(server_host,server_port);
 init_buffer(server);

 if(server==-1)
    msg=PrintMessage(Warning,"Cannot open the Finger connection to %s port %d; [%!s].",server_host,server_port);

 free(server_host);

 return(msg);
}


/*++++++++++++++++++++++++++++++++++++++
  Write to the server to request the URL.

  char *Finger_Request Returns NULL on success, a useful message on error.

  URL *Url The URL to get.

  char *request_head The head of the Finger request for the URL.

  char *request_body The body of the Finger request for the URL.
  ++++++++++++++++++++++++++++++++++++++*/

char *Finger_Request(URL *Url,char *request_head,char *request_body)
{
 char *user,*slash;
 char *msg=NULL;

 /* Take a simple route if it is proxied. */

 if(proxy)
   {
    char *new_request_head=MakeRequestAuthorised(proxy,request_head);

    if(write_string(server,new_request_head)==-1)
       msg=PrintMessage(Warning,"Failed to write to remote Finger %s [%!s].",proxy?"proxy":"server");
    if(request_body && write_string(server,request_body)==-1)
       msg=PrintMessage(Warning,"Failed to write to remote Finger %s [%!s].",proxy?"proxy":"server");

    if(request_head!=new_request_head)
       free(new_request_head);

    return(msg);
   }

 /* Else Sort out the path. */

 user=(char*)malloc(strlen(Url->path));
 strcpy(user,Url->path+1);

 if((slash=strchr(user,'/')))
    *slash=0;

 if(*user)
   {
    if(write_formatted(server,"/W %s\r\n",user)==-1)
       msg=PrintMessage(Warning,"Failed to write to remote Finger server [%!s].");
   }
 else
   {
    if(write_string(server,"/W\r\n")==-1)
       msg=PrintMessage(Warning,"Failed to write to remote Finger server [%!s].");
   }

 free(user);


 bufferhead=HTMLMessageHead(-1,200,"Finger OK",
                            "Content-Type","text/plain",
                            NULL);

 nbufferhead=strlen(bufferhead);
 nreadhead=0;

 return(msg);
}


/*++++++++++++++++++++++++++++++++++++++
  Read a line from the header of the reply for the URL.

  char *Finger_ReadHead Returns the next line of data, NULL on EOF.

  char *line The previous line read.
  ++++++++++++++++++++++++++++++++++++++*/

char *Finger_ReadHead(char *line)
{
 char *l=line;
 int m;

 /* Take a simple route if it is proxied. */

 if(proxy)
    return(read_line_or_timeout(server,line));
 
 /* Else send the header. */

 if(nreadhead==nbufferhead)
    return(NULL);

 for(m=nreadhead;m<nbufferhead;m++)
    if(bufferhead[m]=='\n')
       break;

 m++;
 l=(char*)realloc((void*)l,m-nreadhead+1);
 strncpy(l,&bufferhead[nreadhead],m-nreadhead);
 l[m-nreadhead]=0;

 nreadhead=m;

 return(l);
}


/*++++++++++++++++++++++++++++++++++++++
  Read bytes from the body of the reply for the URL.

  int Finger_ReadBody Returns the number of bytes read on success, -1 on error.

  char *s A string to fill in with the information.

  int n The number of bytes to read.
  ++++++++++++++++++++++++++++++++++++++*/

int Finger_ReadBody(char *s,int n)
{
 return(read_data_or_timeout(server,s,n));
}


/*++++++++++++++++++++++++++++++++++++++
  Close a connection opened using Finger.

  int Finger_Close Return 0 on success, -1 on error.
  ++++++++++++++++++++++++++++++++++++++*/

int Finger_Close(void)
{
 return(CloseSocket(server));

 if(bufferhead)
    free(bufferhead);
}