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
|
/* FILE WRITER HTWrite.c
** ===========
**
*/
#include "HTUtils.h"
#include "tcp.h"
#include "HTWriter.h"
#define BUFFER_SIZE 4096 /* Tradeoff */
/*#include <stdio.h> included by HTUtils.h -- FM */
#include "LYLeaks.h"
#define FREE(x) if (x) {free(x); x = NULL;}
/* HTML Object
** -----------
*/
struct _HTStream {
CONST HTStreamClass * isa;
int soc;
char *write_pointer;
char buffer[BUFFER_SIZE];
#ifdef NOT_ASCII
BOOL make_ascii; /* Are we writing to the net? */
#endif
};
/* Write the buffer out to the socket
** ----------------------------------
*/
PRIVATE void flush ARGS1(HTStream *, me)
{
char *read_pointer = me->buffer;
char *write_pointer = me->write_pointer;
#ifdef NOT_ASCCII
if (me->make_ascii) {
char * p;
for(p = me->buffer; p < me->write_pointer; p++)
*p = TOASCII(*p);
}
#endif
while (read_pointer < write_pointer) {
int status;
status = NETWRITE(me->soc, me->buffer, /* Put timeout? @@@ */
write_pointer - read_pointer);
if (status<0) {
if(TRACE) fprintf(stderr,
"HTWrite: Error: write() on socket returns %d !!!\n", status);
return;
}
read_pointer = read_pointer + status;
}
me->write_pointer = me->buffer;
}
/*_________________________________________________________________________
**
** A C T I O N R O U T I N E S
*/
/* Character handling
** ------------------
*/
PRIVATE void HTWriter_put_character ARGS2(HTStream *, me, char, c)
{
if (me->write_pointer == &me->buffer[BUFFER_SIZE]) flush(me);
*me->write_pointer++ = c;
}
/* String handling
** ---------------
**
** Strings must be smaller than this buffer size.
*/
PRIVATE void HTWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
{
int l = strlen(s);
if (me->write_pointer + l > &me->buffer[BUFFER_SIZE]) flush(me);
strcpy(me->write_pointer, s);
me->write_pointer = me->write_pointer + l;
}
/* Buffer write. Buffers can (and should!) be big.
** ------------
*/
PRIVATE void HTWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
{
CONST char *read_pointer = s;
CONST char *write_pointer = s+l;
flush(me); /* First get rid of our buffer */
while (read_pointer < write_pointer) {
int status = NETWRITE(me->soc, (char *)read_pointer,
write_pointer - read_pointer);
if (status<0) {
if(TRACE) fprintf(stderr,
"HTWriter_write: Error on socket output stream!!!\n");
return;
}
read_pointer = read_pointer + status;
}
}
/* Free an HTML object
** -------------------
**
** Note that the SGML parsing context is freed, but the created object is not,
** as it takes on an existence of its own unless explicitly freed.
*/
PRIVATE void HTWriter_free ARGS1(HTStream *, me)
{
flush(me);
NETCLOSE(me->soc);
FREE(me);
}
PRIVATE void HTWriter_abort ARGS2(HTStream *, me, HTError, e)
{
HTWriter_free(me);
}
/* Structured Object Class
** -----------------------
*/
PRIVATE CONST HTStreamClass HTWriter = /* As opposed to print etc */
{
"SocketWriter",
HTWriter_free,
HTWriter_abort,
HTWriter_put_character, HTWriter_put_string,
HTWriter_write
};
/* Subclass-specific Methods
** -------------------------
*/
PUBLIC HTStream* HTWriter_new ARGS1(int, soc)
{
HTStream* me = (HTStream*)malloc(sizeof(*me));
if (me == NULL) outofmem(__FILE__, "HTML_new");
me->isa = &HTWriter;
#ifdef NOT_ASCII
me->make_ascii = NO;
#endif
me->soc = soc;
me->write_pointer = me->buffer;
return me;
}
/* Subclass-specific Methods
** -------------------------
*/
PUBLIC HTStream* HTASCIIWriter ARGS1(int, soc)
{
HTStream* me = (HTStream*)malloc(sizeof(*me));
if (me == NULL) outofmem(__FILE__, "HTML_new");
me->isa = &HTWriter;
#ifdef NOT_ASCII
me->make_ascii = YES;
#endif
me->soc = soc;
me->write_pointer = me->buffer;
return me;
}
|