I'm trying to develop a ruby based regular expression to parse http response.
My question is: how can I have all the http header names and values as captured groups? I can only capture the last one.
Here is my regex:
/^(?<statusline>HTTP\/(?<protocolversion>\d\.\d) (?<statuscode>\d+) (?<reasonphrase>[^\r\n]+))\r?\n(?:(?<headername>[\w-]+):\s*(?<headervalue>[^\r\n]*)\r?\n)*\r?\n(?<body>[\s\S]*)/m
and my test data:
HTTP/1.1 200 No Content
Date: Mon, 19 Jul 2004 16:18:20 GMT
Server: Apache
Last-Modified: Sat, 10 Jul 2004 17:29:19 GMT
ETag: "1d0325-2470-40f0276f"
Accept-Ranges: bytes
Content-Length: 9328
Connection: close
Content-Type: text/html
<HTML>
<HEAD>
</HEAD>
<BODY>
</BODY>
</HTML>
This regex parses the status line and body properly. But unfortunately I have only the last header parsed, but I would like to have all of them.