Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
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.
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 param1
include an unescaped slash?
Or better yet, state your goal.
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.
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++;
}
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.
As far as I understand the question, using regular expressions can help you.
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 questionAsk a Question
731 491 924 answers to any question