Parser.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  3. * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <AK/StringView.h>
  10. #include <LibURL/URL.h>
  11. namespace URL {
  12. #define ENUMERATE_STATES \
  13. STATE(SchemeStart) \
  14. STATE(Scheme) \
  15. STATE(NoScheme) \
  16. STATE(SpecialRelativeOrAuthority) \
  17. STATE(PathOrAuthority) \
  18. STATE(Relative) \
  19. STATE(RelativeSlash) \
  20. STATE(SpecialAuthoritySlashes) \
  21. STATE(SpecialAuthorityIgnoreSlashes) \
  22. STATE(Authority) \
  23. STATE(Host) \
  24. STATE(Hostname) \
  25. STATE(Port) \
  26. STATE(File) \
  27. STATE(FileSlash) \
  28. STATE(FileHost) \
  29. STATE(PathStart) \
  30. STATE(Path) \
  31. STATE(CannotBeABaseUrlPath) \
  32. STATE(Query) \
  33. STATE(Fragment)
  34. class Parser {
  35. public:
  36. enum class State {
  37. #define STATE(state) state,
  38. ENUMERATE_STATES
  39. #undef STATE
  40. };
  41. static char const* state_name(State const& state)
  42. {
  43. switch (state) {
  44. #define STATE(state) \
  45. case State::state: \
  46. return #state;
  47. ENUMERATE_STATES
  48. #undef STATE
  49. }
  50. VERIFY_NOT_REACHED();
  51. }
  52. // https://url.spec.whatwg.org/#concept-basic-url-parser
  53. static URL basic_parse(StringView input, Optional<URL> const& base_url = {}, Optional<URL> url = {}, Optional<State> state_override = {});
  54. // https://url.spec.whatwg.org/#string-percent-encode-after-encoding
  55. static ErrorOr<String> percent_encode_after_encoding(StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false);
  56. // https://url.spec.whatwg.org/#concept-host-serializer
  57. static ErrorOr<String> serialize_host(Host const&);
  58. // https://url.spec.whatwg.org/#shorten-a-urls-path
  59. static void shorten_urls_path(URL&);
  60. };
  61. #undef ENUMERATE_STATES
  62. }