2

In my visual studio 13 console application, I receive following byte stream on a TCP-Socket(receiving from a embedded device):

POST /setup HTTP/1.1
Content-Length: 6
Content-Type: application/setup+tlv8

TLV8-data

Although it seem to be a valid http request, none of my following attempts have successful been recognize it as a http request: (on regular HTTP Requests they work perfectly)

So far, I'm actually only interested in the requested path from the POST or GET as well as the attached content (tlv formatted) from the body.

Am I wrong in configuring? such as: I need to tell the proper content-type ? Is there any way to get rid of writing a own simple text parser ?

Code sample in case of grapevine:

private void init()
    {
        s = new PairServer();
        s.Host = "172.28.22.78"; 
        s.Port = "52025"; 
        s.Start();
   }

providing following server class:

public class PairServer : RestServer
    {
        [RestRoute(Method = HttpMethod.POST, PathInfo = @"^/setup")]
        [RestRoute(Method = HttpMethod.GET, PathInfo = @"^/setup")]
        public void PairSetup(HttpListenerContext context)
        {
            // will not reach here
        }

        [RestRoute(Method = HttpMethod.POST)]
        public void AnyRoute(HttpListenerContext context)
        {
           // Not even here
        }
Scott Offen
  • 6,933
  • 3
  • 21
  • 24
  • It seems you have a problem with your code. However, we can't help unless we have [code or information that can reproduce the problem](http://stackoverflow.com/help/mcve). Otherwise, we are just blindly guessing. – gunr2171 Oct 31 '14 at 16:48

1 Answers1

1

Although it seem to be a valid http request

No, that's not a valid HTTP request. A valid HTTP request, as the specification states, must include a Host request header:

A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field.

So make sure that your client is following the specification:

POST /setup HTTP/1.1
Host: example.com
Content-Length: 6
Content-Type: application/setup+tlv8

TLV8-data
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • What if I cannot change the http request? So the request always lacks the host header ? Nevermind, your answer explains why regular methods doesn't work. – user3623183 Nov 03 '14 at 17:30
  • @Darin-Dimitrov is correct. Grapevine listens on a protocol/host/port combination using HttpListener, and if your request isn't directed at a specific host (even an IP address should suffice), then it won't ever pick up the request. – Scott Offen Nov 19 '14 at 00:13