/// <summary> /// Called by the server (via the Service method) /// to allow a servlet to handle a OPTIONS request. /// /// The OPTIONS request determines which HTTP methods /// the server supports and /// returns an appropriate header. For example, if a servlet /// overrides DoGet, this method returns the /// following header: /// /// <code>Allow: GET, HEAD, TRACE, OPTIONS</code> /// /// There's no need to override this method unless the /// servlet implements new HTTP methods, beyond those /// implemented by HTTP 1.1. /// </summary> /// <param name="req"> /// the IHttpServletRequest object that /// contains the request the client made of /// the servlet</param> /// <param name="resp"> /// the IHttpServletResponse object that /// contains the response the servlet returns /// to the client /// </param> /// <exception cref="IOException"> /// if an input or output error occurs /// while the servlet is handling the /// OPTIONS request /// </exception> /// <exception cref="ServletException"> /// if the request for the /// OPTIONS cannot be handled /// </exception> protected void DoOptions(IHttpServletRequest req, IHttpServletResponse resp) { MethodInfo[] methods = GetAllDeclaredMethods(this.GetType()); bool ALLOW_GET = false; bool ALLOW_HEAD = false; bool ALLOW_POST = false; bool ALLOW_PUT = false; bool ALLOW_DELETE = false; bool ALLOW_TRACE = true; bool ALLOW_OPTIONS = true; for (int i = 0; i < methods.Length; i++) { MethodInfo m = methods[i]; if (m.Name.Equals("DoGet")) { ALLOW_GET = true; ALLOW_HEAD = true; } if (m.Name.Equals("DoPost")) ALLOW_POST = true; if (m.Name.Equals("DoPut")) ALLOW_PUT = true; if (m.Name.Equals("DoDelete")) ALLOW_DELETE = true; } string allow = null; if (ALLOW_GET) if (allow == null) allow = METHOD_GET; if (ALLOW_HEAD) if (allow == null) allow = METHOD_HEAD; else allow += ", " + METHOD_HEAD; if (ALLOW_POST) if (allow == null) allow = METHOD_POST; else allow += ", " + METHOD_POST; if (ALLOW_PUT) if (allow == null) allow = METHOD_PUT; else allow += ", " + METHOD_PUT; if (ALLOW_DELETE) if (allow == null) allow = METHOD_DELETE; else allow += ", " + METHOD_DELETE; if (ALLOW_TRACE) if (allow == null) allow = METHOD_TRACE; else allow += ", " + METHOD_TRACE; if (ALLOW_OPTIONS) if (allow == null) allow = METHOD_OPTIONS; else allow += ", " + METHOD_OPTIONS; resp.SetHeader("Allow", allow); }
/// <summary> /// The default behavior of this method is to call SetHeader(string name, string value) /// on the wrapped response object. /// </summary> /// <param name="name"></param> /// <param name="value"></param> public void SetHeader(string name, string value) { HttpServletResponse.SetHeader(name, value); }
/// <summary> /// Called by the server (via the Service method) /// to allow a servlet to handle a OPTIONS request. /// /// The OPTIONS request determines which HTTP methods /// the server supports and /// returns an appropriate header. For example, if a servlet /// overrides DoGet, this method returns the /// following header: /// /// <code>Allow: GET, HEAD, TRACE, OPTIONS</code> /// /// There's no need to override this method unless the /// servlet implements new HTTP methods, beyond those /// implemented by HTTP 1.1. /// </summary> /// <param name="req"> /// the IHttpServletRequest object that /// contains the request the client made of /// the servlet</param> /// <param name="resp"> /// the IHttpServletResponse object that /// contains the response the servlet returns /// to the client /// </param> /// <exception cref="IOException"> /// if an input or output error occurs /// while the servlet is handling the /// OPTIONS request /// </exception> /// <exception cref="ServletException"> /// if the request for the /// OPTIONS cannot be handled /// </exception> protected void DoOptions(IHttpServletRequest req, IHttpServletResponse resp) { MethodInfo[] methods = GetAllDeclaredMethods(this.GetType()); bool ALLOW_GET = false; bool ALLOW_HEAD = false; bool ALLOW_POST = false; bool ALLOW_PUT = false; bool ALLOW_DELETE = false; bool ALLOW_TRACE = true; bool ALLOW_OPTIONS = true; for (int i = 0; i < methods.Length; i++) { MethodInfo m = methods[i]; if (m.Name.Equals("DoGet")) { ALLOW_GET = true; ALLOW_HEAD = true; } if (m.Name.Equals("DoPost")) { ALLOW_POST = true; } if (m.Name.Equals("DoPut")) { ALLOW_PUT = true; } if (m.Name.Equals("DoDelete")) { ALLOW_DELETE = true; } } string allow = null; if (ALLOW_GET) { if (allow == null) { allow = METHOD_GET; } } if (ALLOW_HEAD) { if (allow == null) { allow = METHOD_HEAD; } else { allow += ", " + METHOD_HEAD; } } if (ALLOW_POST) { if (allow == null) { allow = METHOD_POST; } else { allow += ", " + METHOD_POST; } } if (ALLOW_PUT) { if (allow == null) { allow = METHOD_PUT; } else { allow += ", " + METHOD_PUT; } } if (ALLOW_DELETE) { if (allow == null) { allow = METHOD_DELETE; } else { allow += ", " + METHOD_DELETE; } } if (ALLOW_TRACE) { if (allow == null) { allow = METHOD_TRACE; } else { allow += ", " + METHOD_TRACE; } } if (ALLOW_OPTIONS) { if (allow == null) { allow = METHOD_OPTIONS; } else { allow += ", " + METHOD_OPTIONS; } } resp.SetHeader("Allow", allow); }