hey Guys i need to split this query to string array how contains three
strings :
query = "Get /index.html HTTP/1.0\r\n\r\n"
query += "Host: www.example.com\r\n"
query += "Connection: Close\r\n\r\n"
i want to get result like
array[0] >>> Get /index.html HTTP/1.0
array[1] >>> Host: www.example.com
array[2] >>> Connection: Close
What have you tried so far, and what are you trying to do? It *looks like* you're dealing with some kind of HTTP request with an extra \r\n between the Get line (method tokens in HTTP/1.1 are case sensitive).
To simply achieve your end as stated you might do:
But if you care about multiple \r\n sequences, which are used to separate the header from the body of an HTTP request, you should think about what you want to do with malformed input.
If you're doing this for your own education and enlightenment then it is a good exercise, if you want to write an application then you might want to look for gems which can process HTTP requests.
Hope this helps,
Mike
···
On 2013-07-04, at 8:59 AM, xcoder Blue_fox <lists@ruby-forum.com> wrote:
hey Guys i need to split this query to string array how contains three
strings :
query = "Get /index.html HTTP/1.0\r\n\r\n"
query += "Host: www.example.com\r\n"
query += "Connection: Close\r\n\r\n"
i want to get result like
array[0] >>> Get /index.html HTTP/1.0
array[1] >>> Host: www.example.com
array[2] >>> Connection: Close
First reaction is to tell you to quit being lazy and go read the docs. Your
example practically in the docs, and you even know the name of the class
and the method. http://rdoc.info/stdlib/core/String:split
Then I realized there are two \r\n after the first line, in which case
maybe your difficulty is that you don't know how to construct the regex
that won't give you an empty line: /\r\n\r\n|\r\n/
But then I realized that there are not two \r\n after the first line of an
http request: HTTP/1.1: Request
your query string is wrong, and I can't figure out what you're
actually
trying to do.
···
On Thu, Jul 4, 2013 at 7:59 AM, xcoder Blue_fox <lists@ruby-forum.com>wrote:
hey Guys i need to split this query to string array how contains three
strings :
query = "Get /index.html HTTP/1.0\r\n\r\n"
query += "Host: www.example.com\r\n"
query += "Connection: Close\r\n\r\n"
i want to get result like
array[0] >>> Get /index.html HTTP/1.0
array[1] >>> Host: www.example.com
array[2] >>> Connection: Close
I am pretty sure you'll find that functionality in Webrick.
Cheers
robert
···
On Thu, Jul 4, 2013 at 2:59 PM, xcoder Blue_fox <lists@ruby-forum.com>wrote:
hey Guys i need to split this query to string array how contains three
strings :
query = "Get /index.html HTTP/1.0\r\n\r\n"
query += "Host: www.example.com\r\n"
query += "Connection: Close\r\n\r\n"
i want to get result like
array[0] >>> Get /index.html HTTP/1.0
array[1] >>> Host: www.example.com
array[2] >>> Connection: Close