我需要解析標頭以從http請求中提取絕對URL。 我只關心的方法只有GET,POST和HEAD。我想確認以下簡單的邏輯/僞代碼將是足以覆蓋大多數情況下,C:HTTP標頭解析
1 url = extract the url from the first request line;
2 if (url[0] == '/') {
3 //relative url, keep reading until HOST or end of the header
4 while (nextline != "\r\n\r\n") {
5 if (nextline starts with HOST) {
6 host = extract the host value;
7 return (host+url);
8 } else {
9 continue;
10 }
11
12 }
13 return bad_req;
14 } else {
15 //absolute url, return directly, ignore HOST
16 return url;
17 }
1)是否有任何情況下離開了?
2)第2行足以說明它是相對URL還是絕對URL?
3)編碼問題?
4)其他可能的故障?
順便說一句,性能是一個非常重要的因素,我需要考慮。
你可以做這個算法更常見,例如[here](https://sourceforge.net/projects/ccgilib/)是我的CGI庫。 –