~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/admin/.svn/text-base/LogTailHandler.ashx.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com
20
 
 |
21
 
 | Author: Mike Lasky (mlasky@novell.com)
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.Collections;
26
 
using System.Collections.Specialized;
27
 
using System.IO;
28
 
using System.Net;
29
 
using System.Text;
30
 
using System.Web;
31
 
using System.Web.Services;
32
 
using System.Web.SessionState;
33
 
 
34
 
namespace Novell.iFolderWeb.Admin
35
 
{
36
 
        /// <summary>
37
 
        /// Summary description for LogTailHandler.
38
 
        /// </summary>
39
 
        public class LogTailHandler : IHttpHandler, IRequiresSessionState
40
 
        {
41
 
                #region Class Members
42
 
 
43
 
                /// <summary>
44
 
                /// Used to log messages.
45
 
                /// </summary>
46
 
                private static readonly iFolderWebLogger log = new iFolderWebLogger(
47
 
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name );
48
 
 
49
 
                /// <summary>
50
 
                /// Maximum read buffer size.
51
 
                /// </summary>
52
 
                private const int BufferSize = 128 * 1024;
53
 
 
54
 
                #endregion
55
 
 
56
 
                #region Private Methods
57
 
 
58
 
                /// <summary>
59
 
                /// Gets the size of the specified file.
60
 
                /// </summary>
61
 
                /// <param name="web">iFolderAdmin object.</param>
62
 
                /// <param name="fileName">The name of the file to get the size for.</param>
63
 
                /// <returns>The current size of the specified file.</returns>
64
 
                private long GetFileSize( iFolderAdmin web, string fileName )
65
 
                {
66
 
                        long fileSize = 0;
67
 
 
68
 
                        UriBuilder uri = new UriBuilder( web.Url );
69
 
                        uri.Path = String.Format( "/simias10/admindata/{0}?size=1", fileName );
70
 
 
71
 
                        HttpWebRequest webRequest = WebRequest.Create( uri.Uri ) as HttpWebRequest;
72
 
                        webRequest.Method = "GET";
73
 
                        webRequest.PreAuthenticate = true;
74
 
                        webRequest.Credentials = web.Credentials;
75
 
                        webRequest.CookieContainer = web.CookieContainer;
76
 
 
77
 
                        HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
78
 
                        try
79
 
                        {
80
 
                                StreamReader sr = new StreamReader( webResponse.GetResponseStream(), Encoding.GetEncoding( "utf-8" ) );
81
 
                                fileSize = Convert.ToInt64( sr.ReadLine() );
82
 
                        }
83
 
                        finally
84
 
                        {
85
 
                                webResponse.Close();
86
 
                        }
87
 
 
88
 
                        return fileSize;
89
 
                }
90
 
 
91
 
                /// <summary>
92
 
                /// Gets a line from the specified buffer. The line must be bounded by an eol
93
 
                /// at the end and at the beginning of the data in order to be a complete line.
94
 
                /// The eol at the beginning of the line is not returned with the data.
95
 
                /// </summary>
96
 
                /// <param name="buffer">Buffer that contains line data.</param>
97
 
                /// <param name="eob">Index to the end of the buffer data.</param>
98
 
                /// <param name="strict">If false line does not need start eol bounds character.</param>
99
 
                /// <returns>A byte array containing a line with a terminating eol char if
100
 
                /// successful. Otherwise an empty buffer is returned.</returns>
101
 
                private byte[] GetLineData( byte[] buffer, int eob, bool strict )
102
 
                {
103
 
                        bool foundEol = false;
104
 
                        byte[] data = new byte[ 0 ];
105
 
                        int eol;
106
 
                        int index;
107
 
 
108
 
                        // If the last chars in the buffer are not eols, discard them until an eol is found.
109
 
                        for( index = eob, eol = 0; index >= 0; --index )
110
 
                        {
111
 
                                // Look for both types of line terminators.
112
 
                                if ( ( buffer[ index ] == '\r' ) || ( buffer[ index ] == '\n' ) )
113
 
                                {
114
 
                                        if ( foundEol == false )
115
 
                                        {
116
 
                                                eol = index;
117
 
                                                foundEol = true;
118
 
                                        }
119
 
                                }
120
 
                                else if ( foundEol )
121
 
                                {
122
 
                                        break;
123
 
                                }
124
 
                        }
125
 
 
126
 
                        // See if any eol chars were found.
127
 
                        if ( index >= 0 )
128
 
                        {
129
 
                                // Need to find another eol before we can count this as a complete line.
130
 
                                int sol;
131
 
                                for ( sol = index; sol >= 0; --sol )
132
 
                                {
133
 
                                        if ( ( buffer[ sol ] == '\r' ) || ( buffer[ sol ] == '\n' ) )
134
 
                                        {
135
 
                                                // Point back at the start of the line.
136
 
                                                ++sol;
137
 
                                                break;
138
 
                                        }
139
 
                                }
140
 
 
141
 
                                // See if the start of the line was found.
142
 
                                if ( sol >= 0 )
143
 
                                {
144
 
                                        int length = ( eol - sol ) + 1;
145
 
                                        data = new byte[ length ];
146
 
                                        Array.Copy( buffer, sol, data, 0, length );
147
 
                                }
148
 
                                else if ( !strict )
149
 
                                {
150
 
                                        int length = eol + 1;
151
 
                                        data = new byte[ length ];
152
 
                                        Array.Copy( buffer, 0, data, 0, length );
153
 
                                }
154
 
                        }
155
 
 
156
 
                        return data;
157
 
                }
158
 
 
159
 
                /// <summary>
160
 
                /// Gets the number of lines to read from the request.
161
 
                /// </summary>
162
 
                /// <param name="request">HttpRequest object</param>
163
 
                /// <returns>The number of lines from the url query string.</returns>
164
 
                private int GetLineCount( HttpRequest request )
165
 
                {
166
 
                        // Default the number of lines.
167
 
                        int lines = 10;
168
 
 
169
 
                        // New requests will contain the number of lines to send back.
170
 
                        NameValueCollection query = request.QueryString;
171
 
                        if ( query[ "lines" ] != null )
172
 
                        {
173
 
                                // This is a new request. Save the request information on the session.
174
 
                                lines = Convert.ToInt32( query[ "lines" ] );
175
 
                                if ( lines > 512 ) lines = 512;
176
 
                        }
177
 
 
178
 
                        return lines;
179
 
                }
180
 
 
181
 
                /// <summary>
182
 
                /// Gets the specified tail data.
183
 
                /// </summary>
184
 
                /// <param name="web">iFolderAdmin object</param>
185
 
                /// <param name="fileName">The name of the file to tail.</param>
186
 
                /// <param name="fileLength">The current length of the file.</param>
187
 
                /// <param name="ti">The tail information saved on the session.</param>
188
 
                /// <param name="length">Receives the total length of the data in the ArrayList.</param>
189
 
                /// <returns>An array of byte arrays containing file data.</returns>
190
 
                private ArrayList GetTailData( iFolderAdmin web, string fileName, long fileLength, TailInfo ti, out int length )
191
 
                {
192
 
                        ArrayList tailLines = new ArrayList( ti.Lines );
193
 
                        length = 0;
194
 
 
195
 
                        // Build the path to the log handler.
196
 
                        UriBuilder uri = new UriBuilder( web.Url );
197
 
                        uri.Path = String.Format( "/simias10/admindata/{0}", fileName );
198
 
 
199
 
                        // Is this a first request?
200
 
                        if ( ti.Offset == -1 )
201
 
                        {
202
 
                                // Have to guess how much data to request.
203
 
                                byte[] buffer = new byte[ ti.Lines * 256 ];
204
 
 
205
 
                                // Read one buffer size from the end of the file.
206
 
                                long readLength = ( fileLength > buffer.Length ) ? buffer.Length : fileLength;
207
 
 
208
 
                                // Calculate the offset to read from.
209
 
                                ti.Offset = fileLength - readLength;
210
 
 
211
 
                                // Add the query string part.
212
 
                                uri.Query = String.Format( "offset={0}&length={1}", ti.Offset, readLength );
213
 
 
214
 
                                // Build the web request to get the data.
215
 
                                HttpWebRequest webRequest = WebRequest.Create( uri.Uri ) as HttpWebRequest;
216
 
                                webRequest.Method = "GET";
217
 
                                webRequest.PreAuthenticate = true;
218
 
                                webRequest.Credentials = web.Credentials;
219
 
                                webRequest.CookieContainer = web.CookieContainer;
220
 
 
221
 
                                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
222
 
                                try
223
 
                                {
224
 
                                        Stream sr = webResponse.GetResponseStream();
225
 
                                        int bytesRead = sr.Read( buffer, 0, ( int )readLength );
226
 
                                        if ( bytesRead > 0 )
227
 
                                        {
228
 
                                                // Get the specified number of lines until the data is all read.
229
 
                                                for ( int lines = 0, eob = bytesRead - 1; 
230
 
                                                          ( lines < ti.Lines ) && ( eob >= 0 ); 
231
 
                                                          ++lines )
232
 
                                                {
233
 
                                                        byte[] line = GetLineData( buffer, eob, true );
234
 
                                                        if ( line.Length > 0 )
235
 
                                                        {
236
 
                                                                tailLines.Add( line );
237
 
                                                                eob -= line.Length;
238
 
                                                                length += line.Length;
239
 
                                                        }
240
 
                                                        else
241
 
                                                        {
242
 
                                                                // No lines exist in the buffer. Return no data.
243
 
                                                                eob = -1;
244
 
                                                        }
245
 
                                                }
246
 
 
247
 
                                                // If any data was returned, update the offset.
248
 
                                                if ( tailLines.Count > 0 )
249
 
                                                {
250
 
                                                        ti.Offset += bytesRead;
251
 
                                                        tailLines.Reverse();
252
 
                                                }
253
 
                                        }
254
 
                                }
255
 
                                finally
256
 
                                {
257
 
                                        webResponse.Close();
258
 
                                }
259
 
                        }
260
 
                        else
261
 
                        {
262
 
                                // See if there is data to read.
263
 
                                long readLength = fileLength - ti.Offset;
264
 
                                if ( readLength > 0 )
265
 
                                {
266
 
                                        // Make sure the read request is not too large.
267
 
                                        if ( readLength > BufferSize )
268
 
                                        {
269
 
                                                readLength = BufferSize;
270
 
                                        }
271
 
 
272
 
                                        // Read to the end of the file.
273
 
                                        byte[] buffer = new byte[ readLength ];
274
 
 
275
 
                                        // Add the query string part.
276
 
                                        uri.Query = String.Format( "offset={0}&length={1}", ti.Offset, readLength );
277
 
 
278
 
                                        // Build the web request to get the data.
279
 
                                        HttpWebRequest webRequest = WebRequest.Create( uri.Uri ) as HttpWebRequest;
280
 
                                        webRequest.Method = "GET";
281
 
                                        webRequest.PreAuthenticate = true;
282
 
                                        webRequest.Credentials = web.Credentials;
283
 
                                        webRequest.CookieContainer = web.CookieContainer;
284
 
 
285
 
                                        HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
286
 
                                        try
287
 
                                        {
288
 
                                                Stream sr = webResponse.GetResponseStream();
289
 
                                                int bytesRead = sr.Read( buffer, 0, ( int )readLength );
290
 
                                                if ( bytesRead > 0 )
291
 
                                                {
292
 
                                                        // Get the specified number of lines until the data is all read.
293
 
                                                        for ( int eob = bytesRead - 1; eob >= 0; )
294
 
                                                        {
295
 
                                                                byte[] line = GetLineData( buffer, eob, false );
296
 
                                                                if ( line.Length > 0 )
297
 
                                                                {
298
 
                                                                        tailLines.Add( line );
299
 
                                                                        eob -= line.Length;
300
 
                                                                        length += line.Length;
301
 
                                                                }
302
 
                                                                else
303
 
                                                                {
304
 
                                                                        // No lines exist in the buffer. Return no data.
305
 
                                                                        eob = -1;
306
 
                                                                }
307
 
                                                        }
308
 
 
309
 
                                                        // If any data was returned, update the offset.
310
 
                                                        if ( tailLines.Count > 0 )
311
 
                                                        {
312
 
                                                                ti.Offset += length;
313
 
                                                                tailLines.Reverse();
314
 
                                                        }
315
 
                                                }
316
 
                                        }
317
 
                                        finally
318
 
                                        {
319
 
                                                webResponse.Close();
320
 
                                        }
321
 
                                }
322
 
                        }
323
 
 
324
 
                        return tailLines;
325
 
                }
326
 
 
327
 
                /// <summary>
328
 
                /// Gets or creates a TailInfo object that contains information about tailing
329
 
                /// the specified file.
330
 
                /// </summary>
331
 
                /// <param name="context">HttpContext object</param>
332
 
                /// <param name="fileName">The name of the file to tail.</param>
333
 
                /// <param name="lines">The number of lines to return on the initial request.</param>
334
 
                /// <returns>A TailInfo object that is saved on the session.</returns>
335
 
                private TailInfo GetTailInfo( HttpContext context, string fileName, int lines )
336
 
                {
337
 
                        TailInfo ti = null;
338
 
 
339
 
                        // Is there already tail information on the session?
340
 
                        Hashtable ht = context.Session[ "TailInfo" ] as Hashtable;
341
 
                        if ( ht == null )
342
 
                        {
343
 
                                context.Session[ "TailInfo" ] = ht = new Hashtable();
344
 
                        }
345
 
 
346
 
                        // See if there is information for this file in the hashtable.
347
 
                        if ( ht.ContainsKey( fileName ) )
348
 
                        {
349
 
                                ti = ht[ fileName ] as TailInfo;
350
 
                        }
351
 
                        else
352
 
                        {
353
 
                                ht[ fileName ] = ti = new TailInfo( lines, -1 );
354
 
                        }
355
 
 
356
 
                        return ti;
357
 
                }
358
 
 
359
 
                #endregion
360
 
 
361
 
                #region IHttpHandler Members
362
 
 
363
 
                /// <summary>
364
 
                /// Enables processing of HTTP Web requests by a custom HttpHandler 
365
 
                /// that implements the IHttpHandler interface.
366
 
                /// </summary>
367
 
                /// <param name="context">An HttpContext object that provides 
368
 
                /// references to the intrinsic server objects (for example, 
369
 
                /// Request, Response, Session, and Server) used to service HTTP requests.</param>
370
 
                public void ProcessRequest( HttpContext context )
371
 
                {
372
 
                        // Setup the response.
373
 
                        HttpRequest request = context.Request;
374
 
                        HttpResponse response = context.Response;
375
 
 
376
 
                        try
377
 
                        {
378
 
                                // Only respond to GET method.
379
 
                                if ( String.Compare( request.HttpMethod, "GET", true ) == 0 )
380
 
                                {
381
 
                                        // The file name of the url is the file that is to be downloaded.
382
 
                                        string fileName = Path.GetFileName( request.Url.LocalPath );
383
 
 
384
 
                                        iFolderAdmin web = context.Session[ "Connection" ] as iFolderAdmin;
385
 
                                        if ( web == null ) context.Response.Redirect( "Login.aspx" );
386
 
 
387
 
                                        // Get the size of the file.
388
 
                                        long fileSize = GetFileSize( web, fileName );
389
 
 
390
 
                                        // New requests will contain the number of lines to send back.
391
 
                                        int lines = GetLineCount( request );
392
 
 
393
 
                                        // Get information about tailing the specified file.
394
 
                                        TailInfo ti = GetTailInfo( context, fileName, lines );
395
 
 
396
 
                                        // Get the line data.
397
 
                                        int length;
398
 
                                        ArrayList lineData = GetTailData( web, fileName, fileSize, ti, out length );
399
 
                                        
400
 
                                        // Setup the response.
401
 
                                        response.Clear();
402
 
                                        response.BufferOutput = false;
403
 
                                        response.Cache.SetCacheability( HttpCacheability.NoCache );
404
 
                                        response.ContentType = "text/plain";
405
 
                                        response.AddHeader("Content-Length", length.ToString() );
406
 
 
407
 
                                        foreach( byte[] line in lineData )
408
 
                                        {
409
 
                                                response.OutputStream.Write( line, 0, line.Length );
410
 
                                        }
411
 
 
412
 
                                        response.Close();
413
 
                                }
414
 
                                else
415
 
                                {
416
 
                                        log.Debug( context, "Error: Invalid http method - {0}", request.HttpMethod );
417
 
                                        response.StatusCode = ( int )HttpStatusCode.BadRequest;
418
 
                                        response.Close();
419
 
                                }
420
 
                        }
421
 
                        catch ( Exception ex )
422
 
                        {
423
 
                                log.Debug( context, "Error: {0}", ex.Message );
424
 
                                log.Debug( context, "Stack trace: {0}", ex.StackTrace );
425
 
                                response.StatusCode = ( int ) HttpStatusCode.InternalServerError;
426
 
                                response.Close();
427
 
                        }
428
 
                }
429
 
 
430
 
                /// <summary>
431
 
                /// Gets a value indicating whether another request can use the IHttpHandler instance.
432
 
                /// </summary>
433
 
                public bool IsReusable
434
 
                {
435
 
                        get { return true; }
436
 
                }
437
 
 
438
 
                #endregion
439
 
 
440
 
                #region TailInfo Class
441
 
 
442
 
                /// <summary>
443
 
                /// Class used to keep track of per file tail information.
444
 
                /// </summary>
445
 
                private class TailInfo
446
 
                {
447
 
                        #region Class Members
448
 
 
449
 
                        /// <summary>
450
 
                        ///  Last offset read from file.
451
 
                        /// </summary>
452
 
                        private long offset;
453
 
 
454
 
                        /// <summary>
455
 
                        /// Number of lines to initially read.
456
 
                        /// </summary>
457
 
                        private int lines;
458
 
 
459
 
                        #endregion
460
 
 
461
 
                        #region Properties
462
 
 
463
 
                        /// <summary>
464
 
                        /// Gets or sets the last offset that the file was read from.
465
 
                        /// </summary>
466
 
                        public long Offset
467
 
                        {
468
 
                                get { return offset; }
469
 
                                set { offset = value; }
470
 
                        }
471
 
 
472
 
                        /// <summary>
473
 
                        /// Gets the number of lines to initially read from the file.
474
 
                        /// </summary>
475
 
                        public int Lines
476
 
                        {
477
 
                                get { return lines; }
478
 
                        }
479
 
 
480
 
                        #endregion
481
 
 
482
 
                        #region Constructor
483
 
 
484
 
                        /// <summary>
485
 
                        /// Constructor
486
 
                        /// </summary>
487
 
                        /// <param name="lines">Number of lines initially requested.</param>
488
 
                        /// <param name="offset">Current read offset of the file.</param>
489
 
                        public TailInfo( int lines, long offset )
490
 
                        {
491
 
                                this.lines = lines;
492
 
                                this.offset = offset;
493
 
                        }
494
 
 
495
 
                        #endregion
496
 
                }
497
 
 
498
 
                #endregion
499
 
        }
500
 
}