Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <windows.h>
- using namespace std;
- string urlDecode(const string &SRC) {
- string result;
- char ch;
- int i, ii;
- for (i = 0; i<SRC.length(); i++) {
- if (int(SRC[i]) == 37) {
- sscanf(SRC.substr(i + 1, 2).c_str(), "%x", &ii);
- ch = static_cast<char>(ii);
- result += ch;
- i = i + 2;
- }
- else {
- result += SRC[i];
- }
- }
- return result;
- }
- string cp1251_to_utf8(const char *str)
- {
- string res;
- int result_u, result_c;
- result_u = MultiByteToWideChar(1251, 0, str, -1, 0, 0);
- if (!result_u)
- return 0;
- wchar_t *ures = new wchar_t[result_u];
- if (!MultiByteToWideChar(1251, 0, str, -1, ures, result_u)) {
- delete[] ures;
- return 0;
- }
- result_c = WideCharToMultiByte(CP_UTF8, 0, ures, -1, 0, 0, 0, 0);
- if (!result_c) {
- delete[] ures;
- return 0;
- }
- char *cres = new char[result_c];
- if (!WideCharToMultiByte(CP_UTF8, 0, ures, -1, cres, result_c, 0, 0)) {
- delete[] cres;
- return 0;
- }
- delete[]ures;
- res.append(cres);
- delete[]cres;
- return res;
- }
- int main()
- {
- cout << cp1251_to_utf8("Content-Type: text/html\n\n<HTML><HEAD><meta charset='UTF-8'><TITLE>Ответ на запрос</TITLE><link rel='stylesheet' href='lab1-4.css'></HEAD>\n<body><div>\n");
- cout << cp1251_to_utf8("<p style='line-height:100%; margin-left:10%;' align='center'><span style='font-size:14pt;'>Cgi-скрипт на С++.</span></p>\n");
- char tempQuery[4096];
- if (GetEnvironmentVariable("QUERY_STRING", tempQuery, 4096))
- {
- string query = tempQuery;
- size_t i, start;
- for (i = 0, start = 0; query[i] != '\0'; i++) {
- if (query[i] == '=') {
- cout << cp1251_to_utf8("<p>Поле ") << urlDecode(query.substr(start, i - start)) << " : ";
- start = i + 1;
- }
- if (query[i] == '&') {
- cout << urlDecode(query.substr(start, i - start)) << "</p>\n";
- start = i + 1;
- }
- }
- cout << urlDecode(query.substr(start, i - start)) << "</p>\n";
- }
- cout << cp1251_to_utf8("</div></BODY></HTML>\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement