source: xmlrpcnet/trunk/fuentes/src/XmlRpcHttpServerProtocol.cs @ 379

Last change on this file since 379 was 379, checked in by hectorgh, 5 years ago

adding project files

File size: 4.1 KB
Line 
1/*
2XML-RPC.NET library
3Copyright (c) 2001-2006, Charles Cook <charlescook@cookcomputing.com>
4
5Permission is hereby granted, free of charge, to any person
6obtaining a copy of this software and associated documentation
7files (the "Software"), to deal in the Software without restriction,
8including without limitation the rights to use, copy, modify, merge,
9publish, distribute, sublicense, and/or sell copies of the Software,
10and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23DEALINGS IN THE SOFTWARE.
24*/
25
26using System;
27using System.Collections;
28using System.IO;
29using System.Reflection;
30using System.Text.RegularExpressions;
31using System.Web.UI;
32
33namespace CookComputing.XmlRpc
34{
35  public class XmlRpcHttpServerProtocol : XmlRpcServerProtocol,
36    IHttpRequestHandler
37  {
38    public void HandleHttpRequest(
39      IHttpRequest httpReq, 
40      IHttpResponse httpResp)   
41    {
42      // GET has its own handler because it can be used to return a
43      // HTML description of the service
44      if (httpReq.HttpMethod == "GET")
45      {
46        XmlRpcServiceAttribute svcAttr = (XmlRpcServiceAttribute)
47          Attribute.GetCustomAttribute(GetType(), typeof(XmlRpcServiceAttribute));
48        if (svcAttr != null && svcAttr.AutoDocumentation == false)
49        {
50          HandleUnsupportedMethod(httpReq, httpResp);
51        }
52        else
53        {
54          bool autoDocVersion = true;
55          if (svcAttr != null)
56            autoDocVersion = svcAttr.AutoDocVersion;
57          HandleGET(httpReq, httpResp, autoDocVersion);
58        }
59        return;
60      }
61      // calls on service methods are via POST
62      if (httpReq.HttpMethod != "POST")
63      {
64        HandleUnsupportedMethod(httpReq, httpResp);
65        return;
66      }
67      //Context.Response.AppendHeader("Server", "XML-RPC.NET");
68      // process the request
69      Stream responseStream = Invoke(httpReq.InputStream);
70      httpResp.ContentType = "text/xml";
71      if (!httpResp.SendChunked)
72      {
73        httpResp.ContentLength = responseStream.Length;
74      }
75      Stream respStm = httpResp.OutputStream;
76      Util.CopyStream(responseStream, respStm);
77      respStm.Flush();
78    }
79
80    protected void HandleGET(
81      IHttpRequest httpReq, 
82      IHttpResponse httpResp,
83      bool autoDocVersion)
84    {
85      using (MemoryStream stm = new MemoryStream())
86      {
87        using (HtmlTextWriter wrtr = new HtmlTextWriter(new StreamWriter(stm)))
88        {
89          XmlRpcDocWriter.WriteDoc(wrtr, this.GetType(), autoDocVersion);
90          wrtr.Flush();
91          httpResp.ContentType = "text/html";
92          if (!httpResp.SendChunked)
93          {
94            httpResp.ContentLength = stm.Length;
95          }
96          stm.Position = 0;
97          Stream respStm = httpResp.OutputStream;
98          Util.CopyStream(stm, respStm);
99          respStm.Flush();
100          httpResp.StatusCode = 200;
101        }
102      }
103    }
104
105    protected void HandleUnsupportedMethod(
106      IHttpRequest httpReq, 
107      IHttpResponse httpResp)
108    {
109      // RFC 2068 error 405: "The method specified in the Request-Line   
110      // is not allowed for the resource identified by the Request-URI.
111      // The response MUST include an Allow header containing a list
112      // of valid methods for the requested resource."
113      //!! add Allow header
114      httpResp.StatusCode = 405;
115      httpResp.StatusDescription = "Unsupported HTTP verb";
116    }
117
118  }
119}
Note: See TracBrowser for help on using the repository browser.