Jestem nowy w C++ i cgicc, i chcę wiedzieć, czy istnieje łatwy sposób na sparsowanie parametrów get do skryptu cgi? ?action=yes&function=no
do tablicy lub coś w tym stylu?cgicc query string parsing
6
A
Odpowiedz
0
Polecam przejrzenie biblioteki boost regex. http://www.boost.org/doc/libs/release/libs/regex/
1
0
Zaprojektowałem program w C++, które mogą pomóc
// (c) 2011 enthusiasticgeek
// The code is adapted from James Marshall's getcgivars.c
// tested with the following webservers
// This code 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.
// Mongoose and LIGHTTPD
// Feel Free to use leave credits intact
// Compile:
// g++ -g testparse.cpp -o testparse.cgi
// To run:
// place cgi file in the appropriate directory depending on server configuration
// Start webserver and then open internet browser and (assuming script running on localserver, port 8080 under 'cgi_bin' directory) enter something similar to
// http://localhost:8080/cgi_bin/testparse.cgi?name1=value1&name2=value2&name3=value3
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <cstdio>
#include <map>
#include <stdlib.h>
#include <cstring>
#include <cctype>
#include <ctype.h>
using namespace std;
vector<string> split(const string& s, const string& delim, const bool keep_empty = true)
{
vector<string> result;
if (delim.empty()) {
result.push_back(s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}
std::map<std::string, std::string>& map_pairs(char* character, std::map<std::string, std::string>& Elements)
{
string test;
string key;
string value;
vector<string>::iterator it;
string character_string = character;
vector<string> words;
//Elements.insert(std::pair<string,string>("0","0"));
//cout << "########## SPLITTING STRING INTO SMALLER CHUNKS ################## " << endl;
words = split(character_string, "&");
//cout << "########## GENERATING KEY VALUE PAIRS ################## " << endl;
for (it=words.begin(); it!=words.end(); ++it)
{
test = *it;
cout<< "string:" << test << endl;
const string::size_type pos_eq = test.find ('=');
if (pos_eq != string::npos)
{
//Assign Key and Value Respectively
key = test.substr (0, pos_eq);
value = test.substr (pos_eq + 1);
//cout << "key = " << key << " and value = " << value << endl;
Elements.insert(std::pair<string,string>(key,value));
}
}
return Elements;
}
std::map<std::string, std::string>& getcgivars(std::map<std::string, std::string>& Elements)
{
register int i ;
char *request_method = NULL;
int content_length;
char *cgiinput = NULL;
// Depending on the request method, read all CGI input into cgiinput
// (really should produce HTML error messages, instead of exit()ing)
request_method= getenv("REQUEST_METHOD") ;
if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD")) {
// Some servers apparently don't provide QUERY_STRING if it's empty,
// so avoid strdup()'ing a NULL pointer here.
char *qs ;
qs= getenv("QUERY_STRING") ;
cgiinput= strdup(qs ? qs : "") ;
}
else if (!strcmp(request_method, "POST")) {
// strcasecmp() is not supported in Windows-- use strcmpi() instead
if (strcasecmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) {
printf("getcgivars(): Unsupported Content-Type.\n") ;
exit(1) ;
}
if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
printf("getcgivars(): No Content-Length was sent with the POST request.\n") ;
exit(1) ;
}
if (!(cgiinput = new char[content_length+1])) {
printf("getcgivars(): Could not \"new\" for cgiinput.\n") ;
exit(1) ;
}
if (!fread(cgiinput, content_length, 1, stdin)) {
printf("Couldn't read CGI input from STDIN.\n") ;
exit(1) ;
}
cgiinput[content_length]='\0' ;
}
else {
printf("getcgivars(): unsupported REQUEST_METHOD\n") ;
exit(1) ;
}
map_pairs(cgiinput,Elements);
return Elements;
}
int main(int argc, char *argv[])
{
int i ;
std::map<std::string, std::string> Elements;
getcgivars(Elements);
// Print the CGI response header, required for all HTML output.
// Note the extra \n, to send the blank line.
printf("Content-type: text/html\n\n") ;
// HTML response page.
printf("<html>\n") ;
printf("<head><title>CGI Results</title></head>\n") ;
printf("<body>\n") ;
printf("<center><h1>CGI FORM TEST</h1></center>\n") ;
printf("<center>HTML form sent the following variables\n");
printf("to the testparse.cgi program:</center>\n") ;
printf("<ul>\n") ;
// Print the CGI variables sent by the user. Note the list of
// variables alternates names and values, and ends in NULL.
for(map<string,string>::iterator ii=Elements.begin(); ii!=Elements.end(); ++ii)
{
cout << "<li>" << (*ii).first << "=" << (*ii).second;
}
printf("</ul>\n") ;
printf("<p>This data is available to you in testparse.cpp<br>\n");
printf("as the contents of \"key\" and \"value\"\n");
printf("</body>\n") ;
printf("</html>\n") ;
Elements.clear();
return 0;
}
10
Oto prosta funkcja stosując regularne biblioteki wyrażenie zawarte w C++ 11.
+0
To wszystko bardzo dobrze, ale od początku 2014 libstdC++ obsługa regex jest zasadniczo nieobecny. –
Powiązane problemy
- 1. LOLCODE String Parsing
- 2. Konwersja String dotychczas w MS Query dostępu
- 3. Python XML Parsing
- 4. Jak zatrzymać Azure IIS Dołączanie error Code Query String
- 5. Json Konwertuj do i od Query String z jquery?
- 6. Java 8 Offset Date Parsing
- 7. Python BeautifulSoup XML Parsing
- 8. PHP EDI X12 Parsing
- 9. python BeautifulSoup parsing table
- 10. JSON Parsing w Androidzie
- 11. Human Name parsing
- 12. Parsing obiekty zagnieżdżone JSON z Json.NET
- 13. Java n-triple RDF parsing
- 14. Parsing YANG Model w java
- 15. Parsing gramatyki przy użyciu OCaml
- 16. Parsing ISO_INSTANT i podobna data Godzina Struny
- 17. ciągi Parsing w formatach niestandardowych wykorzystujące TypeConverter.ConvertFromString()
- 18. Kryteria JPA Query odrębny
- 19. apache ignite query
- 20. Dodatkowe cytaty dodawanych do @query
- 21. Parsing challenge: Notacja dot. Starego logika
- 22. C# GET Request and Parsing JSON
- 23. Parsing inlineImages z Gmaila surowej zawartości
- 24. Parsing .obj model z karty SD Rajawali
- 25. Python XML parsing ze strony internetowej
- 26. Parsing kilka zdań z MaltParser użyciu NLTK
- 27. Parsing numery z double.TryParse dziwne zachowanie
- 28. Parsing Klasy, funkcje i argumenty w PHP
- 29. String Podział pytanie używając "*"
- 30. Serializować Query parametr w Retrofit
To jest całkowicie niezwiązane z twoim pytaniem, ale zauważyłem, że masz na imię TheOnly92 iw tej chwili twoja reputacja ma dokładnie 92 punkty! Heh. Wywołał u mnie uśmiech. –