Non-standard HTTP methods and Apache Http Components

1.9k views Asked by At

I'm writing an HTTP server using the HttpCore library of Apache HTTPComponents 4.3 (Java). My server must be able to receive requests that have non-standard HTTP methods (methods other than GET, POST, DELETE, etc).

But when my server receives such a request, it returns a "method not supported" response. Is there a way to force HTTPComponents to accept non-standard HTTP methods?

Background: I'm working on implementing a WebDAV server, which uses non-standard methods (like MKCOL and PROPFIND).

1

There are 1 answers

0
Michael On BEST ANSWER

I found the solution, so I will answer my own question. xD

You have to create your own HttpRequestFactory implementation, and pass it up the chain.

HttpRequestFactory reqFact = new HttpRequestFactory() {
  public HttpRequest newHttpRequest(final RequestLine requestline) throws MethodNotSupportedException {
    return new BasicHttpEntityEnclosingRequest(requestline);
  }

  public HttpRequest newHttpRequest(final String method, final String uri) throws MethodNotSupportedException {
    return new BasicHttpEntityEnclosingRequest(method, uri);
  }
};
HttpMessageParserFactory<HttpRequest> parserFact = new DefaultHttpRequestParserFactory(null, reqFact);
HttpConnectionFactory<DefaultBHttpServerConnection> connFact = new DefaultBHttpServerConnectionFactory(null, parserFact, null)

The implementation that HttpComponents uses by default throws a MethodNotSupportedException if a non-standard HTTP method is found. The source code for the default implementation can be found here:

http://hc.apache.org/httpcomponents-core-4.3.x/httpcore/xref/org/apache/http/impl/DefaultHttpRequestFactory.html