OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
PollServiceHttpRequest.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Text;
33 using HttpServer;
34 using log4net;
35 using OpenMetaverse;
36 
37 namespace OpenSim.Framework.Servers.HttpServer
38 {
40  {
41  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
42 
44  public readonly IHttpClientContext HttpContext;
45  public readonly IHttpRequest Request;
46  public readonly int RequestTime;
47  public readonly UUID RequestID;
48  public int contextHash;
49 
50  private void GenContextHash()
51  {
52  Random rnd = new Random();
53  contextHash = 0;
54  if (Request.Headers["remote_addr"] != null)
55  contextHash = (Request.Headers["remote_addr"]).GetHashCode() << 16;
56  else
57  contextHash = rnd.Next() << 16;
58  if (Request.Headers["remote_port"] != null)
59  {
60  string[] strPorts = Request.Headers["remote_port"].Split(new char[] { ',' });
61  contextHash += Int32.Parse(strPorts[0]);
62  }
63  else
64  contextHash += rnd.Next() & 0xffff;
65  }
66 
68  PollServiceEventArgs pPollServiceArgs, IHttpClientContext pHttpContext, IHttpRequest pRequest)
69  {
70  PollServiceArgs = pPollServiceArgs;
71  HttpContext = pHttpContext;
72  Request = pRequest;
73  RequestTime = System.Environment.TickCount;
74  RequestID = UUID.Random();
75  GenContextHash();
76  }
77 
78  internal void DoHTTPGruntWork(BaseHttpServer server, Hashtable responsedata)
79  {
80  OSHttpResponse response
81  = new OSHttpResponse(new HttpResponse(HttpContext, Request), HttpContext);
82 
83  byte[] buffer = server.DoHTTPGruntWork(responsedata, response);
84 
85  response.SendChunked = false;
86  response.ContentLength64 = buffer.Length;
87  response.ContentEncoding = Encoding.UTF8;
88  response.ReuseContext = false;
89 
90  try
91  {
92  response.OutputStream.Write(buffer, 0, buffer.Length);
93  response.OutputStream.Flush();
94  response.Send();
95  buffer = null;
96  }
97  catch (Exception ex)
98  {
99  m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", ex);
100  }
101 
102  PollServiceArgs.RequestsHandled++;
103  }
104 
105  internal void DoHTTPstop(BaseHttpServer server)
106  {
107  OSHttpResponse response
108  = new OSHttpResponse(new HttpResponse(HttpContext, Request), HttpContext);
109 
110  response.SendChunked = false;
111  response.ContentLength64 = 0;
112  response.ContentEncoding = Encoding.UTF8;
113  response.ReuseContext = false;
114  response.KeepAlive = false;
115  response.SendChunked = false;
116  response.StatusCode = 503;
117 
118  try
119  {
120  response.OutputStream.Flush();
121  response.Send();
122  }
123  catch (Exception e)
124  {
125  }
126  }
127  }
128 
129  class PollServiceHttpRequestComparer : IEqualityComparer<PollServiceHttpRequest>
130  {
132  {
133  if (b1.contextHash != b2.contextHash)
134  return false;
135  bool b = Object.ReferenceEquals(b1.HttpContext, b2.HttpContext);
136  return b;
137  }
138 
140  {
141  return (int)b2.contextHash;
142  }
143  }
144 }
OSHttpResponse is the OpenSim representation of an HTTP response.
bool Equals(PollServiceHttpRequest b1, PollServiceHttpRequest b2)
PollServiceHttpRequest(PollServiceEventArgs pPollServiceArgs, IHttpClientContext pHttpContext, IHttpRequest pRequest)