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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
Copyright (C) 2002 Cornelius Schumacher <schumacher@kde.org>
Copyright 2006 Michael Pyne <michael.pyne@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <tqdir.h>
#include <tqregexp.h>
#include <kdebug.h>
#include <kprocess.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include <klocale.h>
#include <kconfig.h>
#include "cgi.h"
using namespace KIO;
CgiProtocol::CgiProtocol( const TQCString &pool, const TQCString &app )
: SlaveBase( "cgi", pool, app )
{
kdDebug(7124) << "CgiProtocol::CgiProtocol" << endl;
KConfig cfg( "kcmcgirc" );
cfg.setGroup( "General" );
mCgiPaths = cfg.readListEntry( "Paths" );
}
CgiProtocol::~CgiProtocol()
{
kdDebug(7124) << "CgiProtocol::~CgiProtocol" << endl;
}
/**
* Search in reverse order through a TQByteArray for a given character. The position
* of the character is returned, or -1 if it was not found.
*/
static int qByteArrayFindRev( const TQByteArray &ba, char c, int startIndex )
{
for ( int i = startIndex; i >= 0; --i )
if ( ba[i] == c ) return i;
return -1;
}
/**
* Extract data in ba from start, including no more than len characters from ba.
* Should be exactly comparable to TQCString::mid()
*/
static TQCString extractQCString( const TQByteArray &ba, uint start, uint len = 0xffffffff )
{
uint realLen = len;
if ( ( ba.size() - start ) < len )
realLen = ba.size() - start;
// return TQCString( &ba[ start ], realLen + 1 );
return TQCString( ba ).mid(start, realLen + 1);
}
/**
* Search through a TQByteArray for a given string. The position of the string
* is returned, or -1 if it was not found.
*/
static int qByteArrayFindStr( const TQByteArray &ba, const char *str )
{
int strLen = tqstrlen( str );
int searchLen = ba.size() - strLen;
for ( int i = 0; i <= searchLen; ++i ) {
TQCString temp = extractQCString( ba, i, strLen );
if ( temp == str )
return i;
}
return -1;
}
void CgiProtocol::get( const KURL& url )
{
kdDebug(7124) << "CgiProtocol::get()" << endl;
kdDebug(7124) << " URL: " << url.url() << endl;
#if 0
kdDebug(7124) << " Path: " << url.path() << endl;
kdDebug(7124) << " Query: " << url.query() << endl;
kdDebug(7124) << " Protocol: " << url.protocol() << endl;
kdDebug(7124) << " Filename: " << url.filename() << endl;
#endif
TQCString protocol = "SERVER_PROTOCOL=HTTP";
putenv( protocol.data() );
TQCString requestMethod = "REQUEST_METHOD=GET";
putenv( requestMethod.data() );
TQCString query = url.query().mid( 1 ).local8Bit();
query.prepend( "QUERY_STRING=" );
putenv( query.data() );
TQString path = url.path();
TQString file;
int pos = path.tqfindRev('/');
if ( pos >= 0 ) file = path.mid( pos + 1 );
else file = path;
TQString cmd;
bool stripHeader = false;
bool forwardFile = true;
TQStringList::ConstIterator it;
for( it = mCgiPaths.begin(); it != mCgiPaths.end(); ++it ) {
cmd = *it;
if ( !(*it).endsWith("/") )
cmd += "/";
cmd += file;
if ( KStandardDirs::exists( cmd ) ) {
forwardFile = false;
stripHeader = true;
break;
}
}
FILE *fd;
if ( forwardFile ) {
kdDebug(7124) << "Forwarding to '" << path << "'" << endl;
TQCString filepath = TQFile::encodeName( path );
fd = fopen( filepath.data(), "r" );
if ( !fd ) {
kdDebug(7124) << "Error opening '" << filepath << "'" << endl;
error( KIO::ERR_CANNOT_OPEN_FOR_READING, filepath );
return;
}
} else {
kdDebug(7124) << "Cmd: " << cmd << endl;
fd = popen( TQFile::encodeName(KProcess::quote( cmd )).data(), "r" );
if ( !fd ) {
kdDebug(7124) << "Error running '" << cmd << "'" << endl;
error( KIO::ERR_CANNOT_OPEN_FOR_READING, cmd );
return;
}
}
char buffer[ 4090 ];
while ( !feof( fd ) )
{
int n = fread( buffer, 1, 2048, fd );
if ( n == -1 )
{
// ERROR
if ( forwardFile ) {
fclose( fd );
} else {
pclose( fd );
}
return;
}
buffer[n] = 0;
if ( stripHeader ) {
TQByteArray output;
// Access the buffer in-place by using setRawData()
output.setRawData( buffer, n );
int colon = output.tqfind( ':' );
int newline = output.tqfind( '\n' );
int semicolon = qByteArrayFindRev( output, ';', newline );
int end;
if ( semicolon < 0 ) end = newline;
else end = semicolon;
#if 0
kdDebug(7124) << " colon: " << colon << endl;
kdDebug(7124) << " newline: " << newline << endl;
kdDebug(7124) << " semicolon: " << semicolon << endl;
kdDebug(7124) << " end: " << end << endl;
#endif
TQCString contentType = extractQCString( output, colon + 1, end - colon - 1 );
contentType = contentType.stripWhiteSpace();
kdDebug(7124) << "ContentType: '" << contentType << "'" << endl;
mimeType( contentType );
int start = qByteArrayFindStr( output, "\r\n\r\n" );
if ( start >= 0 ) start += 4;
else {
start = qByteArrayFindStr( output, "\n\n" );
if ( start >= 0 ) start += 2;
}
if ( start < 0 )
start = 0;
// We're done with the part of the buffer we're using.
output.resetRawData ( buffer, n );
// Now access the part of the buffer after the header.
output.setRawData ( buffer + start, n - start );
data( output );
output.resetRawData ( buffer + start, n - start );
stripHeader = false;
} else {
TQByteArray array;
array.setRawData( buffer, n );
data( array );
array.resetRawData( buffer, n );
}
}
if ( forwardFile ) {
fclose( fd );
} else {
pclose( fd );
}
finished();
kdDebug(7124) << "CgiProtocol::get - done" << endl;
}
extern "C" { int KDE_EXPORT kdemain( int argc, char **argv ); }
/*! The kdemain function generates an instance of the ioslave and starts its
* dispatch loop. */
int kdemain( int argc, char **argv )
{
KInstance instance( "kio_cgi" );
kdDebug(7124) << "kio_cgi starting " << getpid() << endl;
if (argc != 4)
{
fprintf(stderr, "Usage: kio_cgi protocol domain-socket1 domain-socket2\n");
exit(-1);
}
CgiProtocol slave( argv[2], argv[3] );
slave.dispatchLoop();
return 0;
}
|