How can we parse HTTP response header fields using Qt/C++? -
i writing piece of software uses qt/kde libs. objective parse http header response fields different fields of struct. far http header response contained in qstring.
it looks this:
"http/1.1 302 found date: tue, 05 jun 2012 07:40:16 gmt server: apache/2.2.22 (linux/suse) x-prefix: 49.244.80.0/21 x-as: 23752 x-mirrorbrain-mirror: mirror.averse.net x-mirrorbrain-realm: region link: <http://download.services.openoffice.org/files/du.list.meta4>; rel=describedby; type="application/metalink4+xml" link: <http://download.services.openoffice.org/files/du.list.torrent>; rel=describedby; type="application/x-bittorrent" link: <http://mirror.averse.net/openoffice/du.list>; rel=duplicate; pri=1; geo=sg link: <http://ftp.isu.edu.tw/pub/openoffice/du.list>; rel=duplicate; pri=2; geo=tw link: <http://ftp.twaren.net/openoffice/du.list>; rel=duplicate; pri=3; geo=tw link: <http://mirror.yongbok.net/openoffice/du.list>; rel=duplicate; pri=4; geo=kr link: <http://ftp.kaist.ac.kr/openoffice/du.list>; rel=duplicate; pri=5; geo=kr digest: md5=b+zfbeizud8exzutwj47xg== digest: sha=a5zw6pkywlhiplffjca+gqiglha= digest: sha-256=hord0mmbzs8ctljpe4pauwstijsnbkaa3gxo4l30eia= location: http://mirror.averse.net/openoffice/du.list content-length: 329 connection: close content-type: text/html; charset=iso-8859-1" in addition custom fields there might few more fields in header response. possible way came manually search fields "link", "digest" , others , create qmap fields keys.however, guess there must better way this. thankful if me.
the http header should in qbytearray (because in ascii, not utf-16), method same qstring:
- split header line line,
- split each line @ colon character,
- trim white spaces (regular spaces ,
'\r'characters) around 2 resulting strings before storing them.
qbytearray httpheaders = ...; qmap<qbytearray, qbytearray> headers; // discard first line httpheaders = httpheaders.mid(httpheaders.indexof('\n') + 1).trimmed(); foreach(qbytearray line, httpheaders.split('\n')) { int colon = line.indexof(':'); qbytearray headername = line.left(colon).trimmed(); qbytearray headervalue = line.mid(colon + 1).trimmed(); headers.insertmulti(headername, headervalue); }
Comments
Post a Comment