Advertisement
AleksandarH

III Parser HTTP

Dec 19th, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void printError(const string& errorMessage, size_t errorPos) {
  8.     cout << "\033[1;31mError: " << errorMessage << " at position " << errorPos << "\033[0m" << endl;
  9. }
  10.  
  11. struct URLComponents {
  12.     string scheme;
  13.     string user;
  14.     string password;
  15.     string host;
  16.     string port;
  17.     string path;
  18. };
  19.  
  20. void parseURL(const string& urlString) {
  21.     URLComponents components;
  22.     size_t schemePos = urlString.find("://");
  23.     if (schemePos == string::npos) {
  24.         printError("Invalid URL syntax: Expected '://' not found in " + urlString, urlString.length());
  25.         return;
  26.     }
  27.     components.scheme = urlString.substr(0, schemePos);
  28.     size_t hostStartPos = schemePos + 3;
  29.     size_t userEndPos = urlString.find(':', hostStartPos);
  30.     size_t hostEndPos = urlString.find('@', hostStartPos);
  31.     size_t portStartPos = urlString.find(':', hostStartPos);
  32.     size_t pathStartPos = urlString.find('/', schemePos + 3);
  33.     if (components.scheme != "http" && components.scheme != "https" && userEndPos != string::npos &&
  34.         (hostEndPos == string::npos || userEndPos < hostEndPos)) {
  35.         components.user = urlString.substr(hostStartPos, userEndPos - hostStartPos);
  36.         components.password = urlString.substr(userEndPos + 1, hostEndPos - userEndPos - 1);
  37.         components.host = urlString.substr(hostEndPos + 1, (portStartPos != string::npos ? portStartPos : pathStartPos) - hostEndPos - 1);
  38.     }
  39.     else {
  40.         components.host = urlString.substr(hostStartPos, (portStartPos != string::npos ? portStartPos : pathStartPos) - hostStartPos);
  41.     }
  42.     if (portStartPos != string::npos && portStartPos < pathStartPos) {
  43.         components.port = urlString.substr(portStartPos + 1, pathStartPos - portStartPos - 1);
  44.     }
  45.     if (pathStartPos != string::npos) {
  46.         components.path = urlString.substr(pathStartPos);
  47.     }
  48.     cout << "Scheme: " << components.scheme << endl;
  49.     if (components.scheme != "http" && components.scheme != "https") {
  50.         cout << "User: " << components.user << endl;
  51.         cout << "Password: " << components.password << endl;
  52.     }
  53.     cout << "Host: " << components.host << endl;
  54.     cout << "Port: " << (components.port.empty() ? "default" : components.port) << endl;
  55.     cout << "Path: " << components.path << endl;
  56. }
  57.  
  58. int main() {
  59.     ifstream file("C:\\Users\\Aleksandar\\source\\repos\\Parser_HTTP\\Parser_HTTP\\input.txt");
  60.     if (!file.is_open()) {
  61.         cout << "Unable to open file!" << endl;
  62.         return 1;
  63.     }
  64.     string line;
  65.     while (getline(file, line)) {
  66.         parseURL(line);
  67.         cout << "-----------------------------" << endl;
  68.     }
  69.     file.close();
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement