Absolute URL from relative path

百般思念 提交于 2019-12-23 09:26:45

问题


I've got a base URL and some relative URIs in that document I want to have the absolute path of.

E.g. base = https://example.com/some/path.html?query=string and relative URIs in that document:

  • index.html → https://example.com/some/index.html
  • .. → https://example.com/
  • ../../../../abc → https://example.com/abc
  • abc/.. → https://example.com/some/
  • //example.org/ → https://example.org/
  • ftp://example.net/ → ftp://example.net/

In Java you have the class URL accomplishing that:

URL abs = new URL(new URL(basePath), relPath);

But strangely enough I can't find a plain C library or function realizing just that.

Is there a library proving that feature? Or better yet some small self-contained file one can use?


回答1:


You can bet this has been written a thousand times in C already. For apache, for example.

Here are some pointers:

libSoup, the http library used by GNOME: http://developer.gnome.org/libsoup/unstable/SoupURI.html#soup-uri-new-with-base

Proposed for Boost libraries: http://cpp-netlib.github.com/

By Google themselves (part of Chrome?): http://code.google.com/p/google-url/

Yet another: http://uriparser.sourceforge.net/

W3C: http://www.w3.org/Library/src/HTParse

URL parsing in libcamel: http://www.google.com/codesearch#KhbZeNk3OGk/camel/camel-url.c

Some more URI parsing APIs, that all seem to not have relative URIs:

GLib, my favorite C library: http://developer.gnome.org/glib/unstable/glib-URI-Functions.html

libedataserver (from Evolution) http://developer.gnome.org/libedataserver/stable/libedataserver-e-url.html

GNet, a glib addon: http://developer.gnome.org/gnet/stable/gnet-uri.html




回答2:


For Windows, you can use CoInternetCombineUrl from Urlmon.dll or UrlCombine from Shlwapi.lib. They do the same AFAIK.



来源:https://stackoverflow.com/questions/8749814/absolute-url-from-relative-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!