S
S
sitev_ru2015-07-06 14:32:55
PHP
sitev_ru, 2015-07-06 14:32:55

Is there a universal URL parsing algorithm?

Should, as I understand it, parse the following:
name1=value1&name2=value2
param1/param2/param3
param1/param2/name1=value1&name2=value2

Answer the question

In order to leave comments, you need to log in

8 answer(s)
A
Alexey Ukolov, 2015-07-06
@alexey-m-ukolov

You're asking for something weird, but given that the URL has a spec , you can say that yes, there is a universal URL parsing algorithm.

V
Viktor Vsk, 2015-07-06
@viktorvsk

Look at any web server and try sending parameters.
What it is is poorly understood.
Take any popular framework and see how the dispatcher works there.
Who told you that param1/param2- this does not param1include an unescaped slash?
Or better yet, state your goal.

T
trevoga_su, 2015-07-06
@trevoga_su

Is there a universal URL parsing algorithm?
no. there is not and cannot be a generic parsing of the path part of a URL. Because you can create absolutely any virtual path.

S
sitev_ru, 2015-07-06
@sitev_ru

I sketched an example (an excerpt from the code), at first it will suit me, maybe it will come in handy for someone:

string path = "param1/param2/name1=value1&name2=value2";

  pos = 0;
  int posEnd = path.length();
  int mode = 1;
  int index = 1;
  string name = "", value = "";

  while (true) {
    if (pos >= posEnd || path[pos] == '/' || path[pos] == '&') {
      if (mode == 1) {
        if (name != "") {
          value = name;
          name = "p" + to_string(index);
          index++;
          params.insert(name, value);
          name = "";
          value = "";
        }
      }
      else {
        params.insert(name, value);
        name = "";
        value = "";
        mode = 1;
      }
      if (pos >= posEnd) break;
    }
    else if (path[pos] == '=') {
      mode = 2;
    }
    else {
      if (mode == 1) name += path[pos]; else value += path[pos];
    }
    pos++;
  }

D
Dmitry Evgrafovich, 2015-07-06
@Tantacula

The best way is to look at how routing is implemented in modern frameworks. In your example, the string is incorrect, it will be correct like this: param1/param2?name1=value1&name2=value2 - the question mark separates the parameters from the path. And what's the point in bicycles if convenient work with a router and many other things is very conveniently implemented before you by much more experienced colleagues in the form of symfony, laravel and other frameworks?
symfony-gu.ru/documentation/ru/html/book/http_fund... - read here, I hope it helps.

D
Dmitry Filatov, 2015-07-06
@i_dozi

As far as I understand the question, using regular expressions can help you.

S
Stanislav Makarov, 2015-07-10
@Nipheris

If you really need a self-written server on the pluses, try to parse the url using this: cpp-netlib.org/0.11.1/in_depth/uri.html#generic-ur... , but in general you can try to take the server from there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question