/// <summary> /// Called by the server (via the Service method) /// to allow a servlet to handle a PUT request. /// /// The PUT operation allows a client to /// place a file on the server and is similar to /// sending a file by FTP. /// /// When overriding this method, leave intact /// any content headers sent with the request (including /// Content-Length, Content-Type, Content-Transfer-Encoding, /// Content-Encoding, Content-Base, Content-Language, Content-Location, /// Content-MD5, and Content-Range). If your method cannot /// handle a content header, it must issue an error message /// (HTTP 501 - Not Implemented) and discard the request. /// For more information on HTTP 1.1, see RFC 2616 /// <a href="http://www.ietf.org/rfc/rfc2616.txt"></a>. /// /// This method does not need to be either safe or idempotent. /// Operations that <code>doPut</code> performs can have side /// effects for which the user can be held accountable. When using /// this method, it may be useful to save a copy of the /// affected URL in temporary storage. /// /// If the HTTP PUT request is incorrectly formatted, /// DoPut returns an HTTP "Bad Request" message. /// </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 /// PUT request /// </exception> /// <exception cref="ServletException">if the request for the PUT cannot be handled</exception> protected void DoPut(IHttpServletRequest req, IHttpServletResponse resp) { string protocol = req.Protocol; //TODO: string msg = lStrings.getString("http.method_put_not_supported"); string msg = "HTTP method PUT is not supported by this URL"; if (protocol.EndsWith("1.1")) { resp.SendError((int)HttpServletResponseStatusCode.SC_METHOD_NOT_ALLOWED, msg); } else { resp.SendError((int)HttpServletResponseStatusCode.SC_BAD_REQUEST, msg); } }
/// <summary> /// The default behavior of this method is to call SendError(int sc, string msg) /// on the wrapped response object. /// </summary> /// <param name="sc"></param> /// <param name="msg"></param> public void SendError(int sc, string msg) { HttpServletResponse.SendError(sc, msg); }
//throws ServletException, IOException /// <summary> /// Receives standard HTTP requests from the public /// Service method and dispatches /// them to the <code>Do</code><i>XXX</i> methods defined in /// this class. This method is an HTTP-specific version of the /// {@link javax.servlet.Servlet#Service} method. There's no /// need to override this method. /// </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 /// HTTP request /// </exception> /// <exception cref="ServletException"> /// if the HTTP request cannot be handled /// </exception> protected void Service(IHttpServletRequest req, IHttpServletResponse resp) { string method = req.Method; if (method.Equals(METHOD_GET)) { long lastModified = GetLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic DoGet(req, resp); } else { long ifModifiedSince = req.GetDateHeader(HEADER_IFMODSINCE); if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less MaybeSetLastModified(resp, lastModified); DoGet(req, resp); } else { resp.Status = (int)HttpServletResponseStatusCode.SC_NOT_MODIFIED; } } } else if (method.Equals(METHOD_HEAD)) { long lastModified = GetLastModified(req); MaybeSetLastModified(resp, lastModified); DoHead(req, resp); } else if (method.Equals(METHOD_POST)) { DoPost(req, resp); } else if (method.Equals(METHOD_PUT)) { DoPut(req, resp); } else if (method.Equals(METHOD_DELETE)) { DoDelete(req, resp); } else if (method.Equals(METHOD_OPTIONS)) { DoOptions(req, resp); } else if (method.Equals(METHOD_TRACE)) { DoTrace(req, resp); } else { // // Note that this means NO servlet supports whatever // method was requested, anywhere on this server. // //TODO: string errMsg = lStrings.getString("http.method_not_implemented"); string errMsg = "Method {0} is not defined in RFC 2068 and is not supported by the Servlet API"; object[] errArgs = new object[1]; errArgs[0] = method; errMsg = string.Format(errMsg, errArgs); resp.SendError((int)HttpServletResponseStatusCode.SC_NOT_IMPLEMENTED, errMsg); } }
/// <summary> /// Receives standard HTTP requests from the public /// Service method and dispatches /// them to the <code>Do</code><i>XXX</i> methods defined in /// this class. This method is an HTTP-specific version of the /// {@link javax.servlet.Servlet#Service} method. There's no /// need to override this method. /// </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 /// HTTP request /// </exception> /// <exception cref="ServletException"> /// if the HTTP request cannot be handled /// </exception> protected void Service(IHttpServletRequest req, IHttpServletResponse resp) //throws ServletException, IOException { string method = req.Method; if (method.Equals(METHOD_GET)) { long lastModified = GetLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic DoGet(req, resp); } else { long ifModifiedSince = req.GetDateHeader(HEADER_IFMODSINCE); if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less MaybeSetLastModified(resp, lastModified); DoGet(req, resp); } else { resp.Status = (int)HttpServletResponseStatusCode.SC_NOT_MODIFIED; } } } else if (method.Equals(METHOD_HEAD)) { long lastModified = GetLastModified(req); MaybeSetLastModified(resp, lastModified); DoHead(req, resp); } else if (method.Equals(METHOD_POST)) { DoPost(req, resp); } else if (method.Equals(METHOD_PUT)) { DoPut(req, resp); } else if (method.Equals(METHOD_DELETE)) { DoDelete(req, resp); } else if (method.Equals(METHOD_OPTIONS)) { DoOptions(req, resp); } else if (method.Equals(METHOD_TRACE)) { DoTrace(req, resp); } else { // // Note that this means NO servlet supports whatever // method was requested, anywhere on this server. // //TODO: string errMsg = lStrings.getString("http.method_not_implemented"); string errMsg = "Method {0} is not defined in RFC 2068 and is not supported by the Servlet API"; object[] errArgs = new object[1]; errArgs[0] = method; errMsg = string.Format(errMsg, errArgs); resp.SendError((int)HttpServletResponseStatusCode.SC_NOT_IMPLEMENTED, errMsg); } }