4 * Author: Teemu Rantanen <tvr@cs.hut.fi>
5 * Copyright (c) 1992 Teemu Rantanen
8 * Created: Thu Apr 16 22:08:27 1992 tvr
9 * Last modified: Wed May 13 13:27:58 1992 tvr
22 #include "../HText/HText.h"
24 extern HText_t *HtLocalText;
27 ClConnection_t *WWWErwiseConnection;
29 void cl_free_connection ();
32 * Where to load this file ?
35 char *WWWErwiseFileLoadName = 0;
38 * Open connection. Allocate strutures.
42 ClOpenConnection (address)
46 ClConnection_t *p = (ClConnection_t *) malloc (sizeof (*p));
49 * Some validation checks
58 memset (p, 0, sizeof (*p));
60 p->address = (char *) strdup (address);
63 * If we wish to load to file, open fd here
66 if (WWWErwiseFileLoadName)
70 open (WWWErwiseFileLoadName, O_WRONLY | O_CREAT, 0666);
72 free (WWWErwiseFileLoadName);
74 WWWErwiseFileLoadName = 0;
76 if (p->load_to_file_fd < 0)
79 printf ("ClOpenConnection: Cannot load to file '%s'\n",
80 WWWErwiseFileLoadName);
82 ClCloseConnection (p);
93 * Call common code to get socket fd. Also set state to some value so
94 * that ReadData calls some function that makes sense.
97 WWWErwiseConnection = p;
99 WWWErwiseStatus = CL_CONTINUES;
101 p->status = HTLoadAbsolute (address, 0);
103 if ((WWWErwiseStatus == CL_FAILED) ||
104 (WWWErwiseStatus == CL_ALREADY_LOADED))
107 ClCloseConnection (p);
117 * Read data or poll connection opening
120 ClReadData (connection, how_done, fd)
121 ClConnection_t *connection;
129 WWWErwiseStatus = CL_CONTINUES;
131 WWWErwiseConnection = connection;
134 * If loading was on non-blocking mode ?
136 if (!connection->function)
139 *fd = connection->fd;
141 if (connection->status)
144 *how_done = CL_COMPLETED;
146 ClCloseConnection (connection);
154 *how_done = CL_FAILED;
156 ClCloseConnection (connection);
164 * Try max 3 pollings at one call. This makes loading faster but does not
167 for (continues = 3; continues > 0; continues--)
170 tmpf = *connection->function;
172 if (*connection->function)
173 (void) (*connection->function) ();
176 * If mode is going to change to poll, set it now
178 if (*connection->function == WWWErwiseSetPoll)
179 (void) WWWErwiseSetPoll ();
182 * Can we try next polling?
184 if ((*connection->function == tmpf) || (*how_done != CL_CONTINUES) ||
185 (connection->select_fd))
192 *how_done = WWWErwiseStatus;
194 *fd = connection->select_fd;
196 if (*how_done == CL_FAILED)
199 ClCloseConnection (connection);
204 if (*how_done == CL_COMPLETED)
207 ClCloseConnection (connection);
209 return HtLocalText; /* global variable because @#$#$ <censored> */
217 * User wants to terminate a connection
221 ClCloseConnection (connection)
222 ClConnection_t *connection;
224 if (connection->load_to_file)
227 close (connection->load_to_file_fd);
233 shutdown (connection->fd, 2);
235 close (connection->fd);
238 if (connection->secondary_fd)
241 shutdown (connection->secondary_fd, 2);
243 close (connection->secondary_fd);
246 cl_free_connection (connection);
252 * read data from local buffer. If no data on buffer, make normal read
255 cl_read_data (fd, data, length)
260 ClConnection_t *p = WWWErwiseConnection;
262 cl_data_t *b = p->buffer_first;
271 if (length < b->size)
274 memcpy (data, b->data, length);
285 memcpy (data, b->data, b->size);
294 p->buffer_first = b->next;
305 return NETREAD (fd, data, length);
313 * Free everything connection structure has malloced (and not yet freed)
317 cl_free_connection (connection)
318 ClConnection_t *connection;
320 if (connection->address)
321 free (connection->address);
323 if (connection->addr)
324 free (connection->addr);
326 if (connection->command)
327 free (connection->command);
329 if (connection->buffer_first)
331 cl_data_t *p = connection->buffer_first;
348 * anAnchor or diag should not be freed (at least they are not on
349 * original common code.
355 * Return true if loading to file is supported with this address
358 ClCanLoadToFile (address)
368 access = HTParse (address, "", PARSE_ACCESS);
370 if (!strcmp (access, "html"))
378 if (!strcmp (access, "file"))
393 * Are we loading this connection to file ?
396 ClConnectionOnLoadToFileMode (connection)
397 ClConnection_t *connection;
402 return connection->load_to_file;
407 ClSetFileNameForLoadingToFile (char *filename)
410 WWWErwiseFileLoadName = (char *) strdup (filename);
412 WWWErwiseFileLoadName = NULL;