Commit d5bfd910 authored by tqcq's avatar tqcq
Browse files

feat add uri

parent 1b3f711b
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -68,7 +68,9 @@ target_sources(
          src/sigslot.cc
          src/status.cc
          src/system_time.cc
          src/time_utils.cc)
          src/time_utils.cc
		  src/uri.cc
)
# set(BUILD_RTTR_DYNAMIC OFF) set(BUILD_UNIT_TESTS OFF)
# set(BUILD_WITH_STATIC_RUNTIME_LIBS ON) set(BUILD_WITH_DOCUMENTATION OFF)
# add_subdirectory(3party/rttr EXCLUDE_FROM_ALL)
@@ -109,6 +111,7 @@ if(SLED_BUILD_TESTS)
        src/system/fiber/fiber_test.cc
        src/system/thread_pool_test.cc
		src/rx_test.cc
		src/uri_test.cc
    )
    target_link_libraries(sled_tests PRIVATE sled GTest::gtest GTest::gtest_main)
    add_test(NAME sled_tests COMMAND sled_tests)

include/sled/uri.h

0 → 100644
+55 −0
Original line number Diff line number Diff line
#pragma once
#include <map>
#ifndef SLED_URI_H
#define SLED_URI_H
#include <string>

namespace sled {
namespace internal {
#define __SLED_URI_GETTER_AND_SETTER(type, name)                                                                       \
    type &name() & { return name##_; }                                                                                 \
    type &&name() && { return std::move(name##_); }                                                                    \
    type const &name() const & { return name##_; }                                                                     \
    void set_##name(type const &v) { name##_ = v; }                                                                    \
    void set_##name(type &&v) { name##_ = std::move(v); }

}// namespace internal

class URI {
public:
    // using ParamType = std::pair<std::string, std::string>;
    using ParamMap = std::map<std::string, std::string>;
    // http://xxx.com/index.html?field=value
    static URI ParseAbsoluteURI(const std::string &uri);
    // http://xxx.com/index.html?field=value#download
    static URI ParseURI(const std::string &uri);
    // http://xxx.com/index.html
    static URI ParseURIReference(const std::string &uri);

    // setter getter
    __SLED_URI_GETTER_AND_SETTER(std::string, scheme)
    __SLED_URI_GETTER_AND_SETTER(std::string, username)
    __SLED_URI_GETTER_AND_SETTER(std::string, password)
    __SLED_URI_GETTER_AND_SETTER(std::string, host)
    __SLED_URI_GETTER_AND_SETTER(std::string, port)
    __SLED_URI_GETTER_AND_SETTER(std::string, path)
    __SLED_URI_GETTER_AND_SETTER(std::string, file_name)
    __SLED_URI_GETTER_AND_SETTER(std::string, file_extension)
    __SLED_URI_GETTER_AND_SETTER(ParamMap, query)
    __SLED_URI_GETTER_AND_SETTER(std::string, anchor)

private:
    std::string scheme_;
    std::string username_;
    std::string password_;
    std::string host_;
    std::string port_;
    std::string path_;
    std::string file_name_;
    std::string file_extension_;
    ParamMap query_;
    std::string anchor_;
};
}// namespace sled

#endif//  SLED_URI_H

src/uri.cc

0 → 100644
+16 −0
Original line number Diff line number Diff line
#include "sled/uri.h"
#include "sled/strings/utils.h"

namespace sled {
URI
URI::ParseURI(const std::string &uri_str)
{
    if (uri_str.empty()) { return {}; }
    URI uri;
    // TODO: decode before
    auto start_pos = uri_str.find_first_not_of(" ");
    auto end_pos = uri_str.find(':');

    return std::move(uri);
}
}// namespace sled

src/uri_test.cc

0 → 100644
+2 −0
Original line number Diff line number Diff line
#include <gtest/gtest.h>
#include <sled/uri.h>