Created
July 8, 2013 16:05
-
-
Save jackbergus/5950137 to your computer and use it in GitHub Desktop.
An easy way to parse http headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* parse_commands_http.cpp | |
* This file is part of parse_commands_http | |
* | |
* Copyright (C) 2013 - Giacomo Bergami | |
* | |
* parse_commands_http is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* parse_commands_http is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with parse_commands_http. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <algorithm> | |
#include <iterator> | |
#include <unordered_map> | |
extern "C" { | |
#include <string.h> | |
} | |
using namespace std; | |
unordered_map<string,string> parse_commands_http(string sentence) { | |
unordered_map<string,string> test; | |
istringstream iss(sentence); | |
char buffer_test[300]; | |
memset(buffer_test,0,300); | |
while (iss.getline(buffer_test,300,'\n')) { | |
char buffer_test2[300]; | |
memset(buffer_test2,0,300); | |
string command{buffer_test}; | |
istringstream iss(command); | |
if (iss.getline(buffer_test2,300,':')) { | |
string command{buffer_test2}; | |
if (!iss.getline(buffer_test2,300,':')) | |
continue; | |
else { | |
string attribute{buffer_test2}; | |
test[command] = attribute; | |
} | |
} | |
memset(buffer_test,0,300); | |
} | |
return test; | |
} | |
int main() { | |
string test = "HTTP/1.1 200 OK\nDate: Mon, 12 Mar 2001 19:12:16 GMT\nServer: Apache/1.3.12 (Unix) Debian/GNU mod_perl/1.24\nLast-Modified: Fri, 22 Sep 2000 14:16:18\nETag: \"dd7b6e-d29-39cb69b2\"\nAccept-Ranges: bytes\nContent-Length: 3369\nConnection: close\nContent-Type: text/html"; | |
unordered_map<string,string> result{parse_commands_http(test)}; | |
cout << result["Servero"].length() << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment