Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/cs/vcs_src/ifs_http.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
/*
21
* File: ifs_httpcs.c
22
*/
23
24
#include "ifs_agent.h"
25
#include <stdio.h>
26
27
struct {
28
int version;
29
} HttpData;
30
31
/*
32
*name: HttpConvert
33
* create a file of reference entries from the HTML document.
34
*/
35
int
36
HttpConvert( srcfile, htmlfile, linkfile )
37
char *srcfile;
38
char *htmlfile;
39
char *linkfile;
40
{
41
FILE *fsrc, *fhtml, *flink;
42
char buf[ STRLEN ];
43
char *ptr, *p1, *p2;
44
int len;
45
int textflag = 0;
46
47
if( (fsrc = fopen( srcfile, "r" )) == NULL ) {
48
return -1;
49
}
50
if( (fhtml = fopen( htmlfile, "w" )) == NULL ) {
51
fclose( fsrc );
52
return -1;
53
}
54
if( (flink = fopen( linkfile, "w" )) == NULL ) {
55
fclose( fhtml );
56
fclose( fsrc );
57
return -1;
58
}
59
60
/* parse header */
61
while( fgets( buf, sizeof(buf), fsrc ) != NULL ) {
62
fprintf( flink, "%s", buf );
63
if( buf[0] == '\n' || buf[0] == '\r' )
64
break;
65
if( strncmp( buf, "Content-Type: text", 18 ) == 0 )
66
textflag = 1;
67
}
68
69
if( textflag ) {
70
/* parse htmlfile */
71
while( fgets( buf, sizeof(buf), fsrc ) != NULL ) {
72
fprintf( fhtml, "%s", buf );
73
ptr = buf;
74
while( (p1 = strchr( ptr, '<' )) != NULL &&
75
(p2 = strchr( p1, '>' )) != NULL ) {
76
p1++;
77
*p2++ = '\0';
78
79
/* <a href="..."> */
80
if( (*p1 == 'a' || *p1 == 'A') && p1[1] == ' ' )
81
fprintf( flink, "<%s>\n", p1 );
82
ptr = p2;
83
}
84
}
85
} else {
86
/* dump binary data */
87
while( (len = fread( buf, 1, sizeof(buf), fsrc )) > 0 )
88
fwrite( buf, 1, len, fhtml );
89
}
90
91
fclose( flink );
92
fclose( fhtml );
93
fclose( fsrc );
94
return 0;
95
}
96
97
/*
98
*name: HttpXfer
99
* transfer a file from web server
100
*/
101
int
102
HttpXfer( srv, rpath, linkfile, tmpfile )
103
struct server_info *srv;
104
char *rpath;
105
char *linkfile;
106
char *tmpfile;
107
{
108
struct mount_item *mitem = srv->mitem;
109
NetFile *nFile;
110
FILE *fout;
111
char buf[ STRLEN ], *ptr;
112
char line[ STRLEN ];
113
int port, len, ans = -1;
114
115
if( (port = mitem->port) <= 0 )
116
port = 80;
117
if( (fout = fopen( tmpfile, "w" )) == NULL ) {
118
logit( "<http>: tmpfile open error\n" );
119
return -1;
120
}
121
if( (nFile = NetConnect( srv, mitem->host, port )) == NULL ) {
122
fclose( fout );
123
logit( "<http>: connect error\n" );
124
return -1;
125
}
126
sfsprintf( buf, sizeof(buf), "GET %s HTTP/1.0\n", rpath );
127
NetWrite( nFile, buf, strlen(buf) );
128
logit( buf );
129
130
#if 0 /* -------- ignore this feature -------- */
131
/* If-Modified-Since: <Last-Modified> */
132
if( (fp = fopen( linkfile, "r" )) != NULL ) {
133
while( fgets( line, sizeof(line), fp ) != NULL && line[0] != '\n' ) {
134
if( strncasecmp( line, "Last-Modified: ", 15 ) == 0 ) {
135
sfsprintf( buf, sizeof(buf), "If-Modified-Since: %s", line+15 );
136
NetWrite( nFile, buf, strlen(buf) );
137
debug_logit( buf );
138
break;
139
}
140
}
141
fclose( fp );
142
}
143
#endif
144
145
/* Cookie: <cookie-value> */
146
if( DataEntryQuery( srv->lpath, "cookie", line, sizeof(line) ) > 0 ) {
147
sfsprintf( buf, sizeof(buf), "Cookie: %s\n", line );
148
NetWrite( nFile, buf, strlen(buf) );
149
debug_logit( buf );
150
}
151
152
ptr = "Accept: */*\n\n";
153
NetWrite( nFile, ptr, strlen(ptr) );
154
debug_logit( ptr );
155
156
/* reply: HTTP/1.0 200 Document follows */
157
NetGets( nFile, line, sizeof(line) );
158
logit( line );
159
if( (ptr = strchr( line, ' ' )) != NULL ) {
160
ans = (int)strtol( ptr+1, (char**)0, 0 );
161
}
162
fputs( line, fout );
163
while( (len = NetRead( nFile, line, sizeof(line) )) > 0 ) {
164
fwrite( line, 1, len, fout );
165
}
166
NetClose( nFile );
167
fclose( fout );
168
return ans;
169
}
170
171
/*
172
*name: HttpXferDir
173
* transfer URL as a directory
174
*/
175
int
176
HttpXferDir( srv, tmpfile )
177
struct server_info *srv;
178
char *tmpfile;
179
{
180
char rpath[ STRLEN ];
181
char linkfile[ STRLEN ];
182
char *lpath = srv->lpath;
183
int ans;
184
185
sfsprintf( rpath, sizeof(rpath), "%s/", srv->rpath );
186
sfsprintf( linkfile, sizeof(linkfile), "%s/._dir", lpath );
187
ans = HttpXfer( srv, rpath, linkfile, tmpfile );
188
if( ans < 300 ) { /* 2xx Successful, 1xx Informational */
189
if( chdir( lpath ) ) {
190
MakePath( linkfile );
191
if( chdir( lpath ) )
192
return 404; /* 404 Not Found */
193
}
194
HttpConvert( tmpfile, "index.html", "._dir" );
195
symlink( srv->mitem->timeout, "._cache_time" );
196
}
197
return ans;
198
}
199
200
/*
201
*name: HttpXferFile
202
* transfer URL as a text file
203
*/
204
int
205
HttpXferFile( srv, tmpfile )
206
struct server_info *srv;
207
char *tmpfile;
208
{
209
char linkfile[ STRLEN ];
210
char *lpath = srv->lpath;
211
char *ptr;
212
int ans;
213
214
if( (ptr = strrchr( lpath, '/' )) == NULL )
215
return -1;
216
*ptr = '\0';
217
sfsprintf( linkfile, sizeof(linkfile), "%s/._dir.%s", lpath, ptr+1 );
218
*ptr = '/';
219
220
ans = HttpXfer( srv, srv->rpath, linkfile, tmpfile );
221
if( ans < 300 ) { /* 2xx Successful, 1xx Informational */
222
MakePath( lpath );
223
HttpConvert( tmpfile, lpath, linkfile );
224
}
225
return ans;
226
}
227
228
/*
229
*name: HttpGetFile
230
* validate a URL file
231
*/
232
int
233
HttpGetFile( srv )
234
struct server_info *srv;
235
{
236
char tmpfile[ STRLEN ];
237
char *rpath, *ptr;
238
int ans;
239
240
MakeTmpFile( srv->lpath, tmpfile, sizeof(tmpfile) );
241
rpath = srv->rpath;
242
if( (ptr = strrchr( rpath, '/' )) != NULL &&
243
strcmp( ptr, "/index.html" ) == 0 ) {
244
/* try get the directory html file (.../index.html) */
245
*ptr = '\0';
246
ptr = strrchr( srv->lpath, '/' );
247
*ptr = '\0';
248
}
249
if( *rpath == '\0' || DashD( srv->lpath ) ) {
250
ans = HttpXferDir( srv, tmpfile );
251
} else if( DashF( srv->lpath ) ) {
252
ans = HttpXferFile( srv, tmpfile );
253
} else {
254
ans = HttpXferFile( srv, tmpfile );
255
if( ans >= 400 ) { /* 4xx, 5xx (ex: 404 Not Found) */
256
return -1;
257
} else if( ans >= 300 ) { /* 3xx Redirection */
258
ans = HttpXferDir( srv, tmpfile );
259
}
260
}
261
unlink( tmpfile );
262
return( ans >= 400 ? -1 : 0 );
263
}
264
265
/*
266
*name: HttpUserDef
267
* query/modify the extra header of http protocol.
268
* (ex: Cookie ...)
269
*/
270
int
271
HttpUserDef( srv, argc, argv )
272
struct server_info *srv;
273
int argc;
274
char *argv[];
275
{
276
char buf[ STRLEN ];
277
char *fpath, *key, *data;
278
279
if( argc < 2 ) {
280
sfsprintf( csusrmsg, sizeof(csusrmsg), "1 Usage: userdef local-path key (-|data)" );
281
return 0;
282
}
283
fpath = argv[0];
284
key = argv[1];
285
if( argc < 3 ) { /* query data */
286
if( DataEntryQuery( fpath, key, buf, sizeof(buf) ) >= 0 ) {
287
sfsprintf( csusrmsg, sizeof(csusrmsg), "0 %s %s %s", fpath, key, buf );
288
} else {
289
sfsprintf( csusrmsg, sizeof(csusrmsg), "1 %s %s not-found", fpath, key );
290
}
291
} else if( *argv[2] == '-' ) { /* delete header */
292
if( DataEntryDelete( fpath, key ) == 0 ) {
293
sfsprintf( csusrmsg, sizeof(csusrmsg), "0 %s %s deleted", fpath, key );
294
} else {
295
sfsprintf( csusrmsg, sizeof(csusrmsg), "1 %s %s not-found", fpath, key );
296
}
297
} else {
298
data = argv[2];
299
DataEntryInsert( fpath, key, data, strlen(data)+1 );
300
sfsprintf( csusrmsg, sizeof(csusrmsg), "0 %s %s inserted", fpath, key );
301
}
302
return 0;
303
}
304
305
int
306
HttpNop()
307
{
308
return 0;
309
}
310
311
int
312
HttpInit( tbl )
313
struct agent_item *tbl;
314
{
315
tbl->localdata = (char *) &HttpData;
316
tbl->connect = HttpNop;
317
tbl->disconnect = HttpNop;
318
tbl->listdents = HttpGetFile;
319
tbl->getfile = HttpGetFile;
320
tbl->putfile = HttpNop;
321
tbl->userdef = HttpUserDef;
322
return 0;
323
}
324
325
326