summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htnet/HtNNTP.cc
blob: 2a79bf4110840b529c469c9004f0695ed69342d6 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
//
// HtNNTP.cc
//
// HtNNTP: Interface classes for NNTP messaging
//
// Gabriele Bartolini - Prato - Italia
// started: 01.08.2000
//
// Including:
// 	 -  Generic class
// 	 -  Response message class
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 2000-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtNNTP.cc,v 1.5 2004/05/28 13:15:23 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "lib.h"
#include "Transport.h"
#include "HtNNTP.h"

#include <signal.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>      // for sscanf

// for setw()
#ifdef HAVE_STD
#include <iomanip>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iomanip.h>
#endif /* HAVE_STD */

#if 1
typedef void (*SIGNAL_HANDLER) (...);
#else
typedef SIG_PF SIGNAL_HANDLER;
#endif

   // Stats information
   	 int HtNNTP::_tot_seconds = 0;
   	 int HtNNTP::_tot_requests = 0;
   	 int HtNNTP::_tot_bytes = 0;

///////
   //    HtNNTP_Response class
   //
   //    Response message sent by the remote NNTP server
///////


// Construction

HtNNTP_Response::HtNNTP_Response()
{

}


// Destruction

HtNNTP_Response::~HtNNTP_Response()
{
}


void HtNNTP_Response::Reset()
{

   // Call the base class method in order to reset
   // the base class attributes

   Transport_Response::Reset();

}




///////
   //    HtNNTP generic class
   //
   //
///////


// Construction

HtNNTP::HtNNTP()
: Transport(new Connection()),
   _bytes_read(0),
   _useproxy(0)
{
}

// Destruction

HtNNTP::~HtNNTP()
{
  // Free the connection
  //
  CloseConnection();
  if (_connection)
    delete _connection;
  _connection = 0;
}


///////
   //    Manages the requesting process
///////

Transport::DocStatus HtNNTP::Request()
{

   DocStatus result = Document_ok;
   _response.Reset();   // Reset the response

   return result;

}


void HtNNTP::SetRequestCommand(String &cmd)
{

   cmd << "\r\n";

}




//*****************************************************************************
// int HtNNTP::ParseHeader()
//   Parse the header of the document
//
int HtNNTP::ParseHeader()
{
    String	line = 0;
    int		inHeader = 1;

    if (_response._modification_time)
    {
	delete _response._modification_time;
	_response._modification_time=NULL;
    }
    while (inHeader)
    {

      line.trunc();

      if(! _connection->Read_Line(line, "\n"))
         return -1;  // Connection down
	
      _bytes_read+=line.length();
      line.chop('\r');

      if (line.length() == 0)
         inHeader = 0;
      else
      {
         // Found a not-empty line
	
         if (debug > 3)
            cout << "Header line: " << line << endl;
	
         // Status - Line check
         char	*token = line.get();

         while (*token && !isspace(*token))
            token++;

         while (*token && isspace(*token))
            token++;
      }
    }

    if (_response._modification_time == NULL)
    {
      if (debug > 3)
         cout << "No modification time returned: assuming now" << endl;

         //Set the modification time
      _response._modification_time = new HtDateTime;
      _response._modification_time->ToGMTime(); // Set to GM time

    }

    return 1;

}


HtNNTP::DocStatus HtNNTP::GetDocumentStatus(HtNNTP_Response &r)
{

   // Let's give a look at the return status code

   HtNNTP::DocStatus returnStatus=Document_not_found;
   int statuscode;

   statuscode=r.GetStatusCode();

   if(statuscode==200)
   {
	    returnStatus = Document_ok;   // OK
   }

   // Exit the function
   return returnStatus;
	
}


int HtNNTP::ReadBody()
{

    _response._contents = 0;	// Initialize the string

    char	docBuffer[8192];
    int		bytesRead = 0;
    int		bytesToGo = _response._content_length;

    if (bytesToGo < 0 || bytesToGo > _max_document_size)
        bytesToGo = _max_document_size;

    if( _connection == NULL )
      {
	cout << "HtNNTP::ReadBody: _connection is NULL\n";
	exit(0);
      }


    while (bytesToGo > 0)
    {
        int len = bytesToGo< (int)sizeof(docBuffer) ? bytesToGo : (int)sizeof(docBuffer);
        bytesRead = _connection->Read(docBuffer, len);
        if (bytesRead <= 0)
            break;

	_response._contents.append(docBuffer, bytesRead);

	bytesToGo -= bytesRead;
	
	_bytes_read+=bytesRead;

    }

    // Set document length
    _response._document_length = _response._contents.length();

   return bytesRead;

}


///////
   //    Show the statistics
///////

ostream &HtNNTP::ShowStatistics (ostream &out)
{
   Transport::ShowStatistics(out);  // call the base class method

   out << " NNTP Requests             : " << GetTotRequests() << endl;
   out << " NNTP KBytes requested     : " << (double)GetTotBytes()/1024 << endl;
   out << " NNTP Average request time : " << GetAverageRequestTime()
      << " secs" << endl;

   out << " NNTP Average speed        : " << GetAverageSpeed()/1024
      << " KBytes/secs" << endl;

   return out;
}