cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

Accessing Directory

Open th-baer opened this issue 1 year ago • 4 comments

Hello,

I was testing you library and I had a problem to access a directory http://localhost/dir
Looking into the code I noticed that you check for / at the end so http://localhost/dir/ did work.

I would suggest to change the code to handle this like:

inline bool Server::handle_file_request(const Request &req, Response &res,
                                        bool head) {
  for (const auto &entry : base_dirs_) {
    // Prefix match
    if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) {
      std::string sub_path = "/" + req.path.substr(entry.mount_point.size());
      if (detail::is_valid_path(sub_path)) {
        auto path = entry.base_dir + sub_path;
        if (path.back() == '/') { path += "index.html"; }

        else if (detail::is_dir(path)) { path += "/index.html"; }

Only the last line was added.

Best Regadrs, Thomas

th-baer avatar Sep 08 '22 09:09 th-baer

@th-baer, thanks for the feedback. I understand what you would like to do, but I don't prefer the solution to take /dir as /dir/. I think most HTTP servers (like 'python -m http.server') use redirection (301) from /dir to /dir/. I'll take a look at the approach instead.

yhirose avatar Sep 08 '22 13:09 yhirose

@yhirose I completely agree with that redirect is the standard solution. I hope it's not too much work,

th-baer avatar Sep 08 '22 14:09 th-baer

Could you assign this issue to me .I'm new to Open source Contribution and I'd like to give it a shot.

the-WINTERSOLDIER avatar Oct 15 '22 11:10 the-WINTERSOLDIER

Could you assign this issue to me .I'm new to Open source Contribution and I'd like to give it a shot.

Why asking ? go fork repo, do contribution and create pull request here. lol

graysuit avatar Oct 15 '22 15:10 graysuit