-1

I am working with a server written in Active State Perl. Apparently, if a GET request is poorly formed, response results will return, but without their http headers. That is true in my case.

The Perl server listens for requests and send response using IO::Socket and SendHTTPRequest respectively.

So my question(s) are as follows. Is the following GET request invalid?

"GET ?CA=1&STREET=990%20MASS%20AVENUE&STREET2=UNIT%20A37&CITY=ARLINGTON&STATE=MA&ZIP=00000%20HTTP/1.1\r\n\r\n"

If the request is invalid how is it invalid? I would like to correct the format.

Apparently, ActiveState Perl thinks the request is not valid.

After the socket is correctly set up, the following code sends the request and waits for a response. If you need to see the socket setup including the host name, I'll edit this post and add at the bottom, but assume the socket is set up correctly.

size_t bytes_read = 0; /* Keeps per-loop read-count. */
char local_buf[INET_BUF_LEN] = {'\0'};
total_socket_read_bytesM = 0; /* Initialize module-scope read length */
write (socket_fdP, request_bufP, strlen(request_bufP));

while(TRUE)
{
  if(total_socket_read_bytesM  <= (read_bufP_len - 1))
  {
    bytes_read = read(socket_fdP, (local_buf + bytes_read), sizeof(local_buf));

    if (bytes_read == 0)
    {
      break;
    }
    else
    {
     total_socket_read_bytesM += bytes_read;
    }
  }
  else
  {
    break;
  }
}

I could punt, and not worry given I'm getting results back. However, I would like to write my C program to create a valid http GET request.

Epilogue:

This is what my working request looks like:

"GET /?CA=1&STREET=990%20MASS%20AVENUE&STREET2=UNIT%20A37&CITY=ARLINGTON&STATE=MA&ZIP=00000 HTTP/1.1\r\n\r\n"

Lorkenpeist
  • 1,475
  • 2
  • 13
  • 26
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131

1 Answers1

3

The "Host" request field is required in HTTP/1.1, so your request is indeed invalid.

%20HTTP/1.1 is wrong too. There must be an actual space character after the resource name and before the HTTP version, not an escaped space.

I'm uncertain (and too lazy to check) whether the resource identifier can begin with ? in a request. If not then it should be /?CA=1....

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • When you say Host request field, where should that be in the string, right before the `/?CA=1` ? – octopusgrabbus Mar 12 '13 at 15:16
  • 1
    That URI is indeed invalid. In some cases it's acceptable to use an absolute URI (eg. "GET http://stackoverflow.com/ http/1.1... ... ..." is what a HTTP proxy request would look like). In most cases an absolute path is used (eg. `"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"` for a request for google's homepage). In some cases a '*' can be used, when the request isn't for a resource. There's one other exception: the CONNECT method, which doesn't apply here. – autistic Mar 12 '13 at 15:24
  • 1
    @octopusgrabbus See my google example for an answer to that question. By the way, which specification are you reading? You could be slack enough to expect SO to do your research for you, but that'd be quite inoptimal. – autistic Mar 12 '13 at 15:24
  • No plans for anyone to do my research. – octopusgrabbus Mar 12 '13 at 15:32