← 返回 airbnb 的题目列表URL Parser with Percent Unquote
类型:online_judge
Problem: Implement a URL Parser and an unquote API
Given several URL strings, implement a simplified URL parser that extracts the following fields for each URL:
scheme: protocol name, such as http or https; null if missing
host: host name
port: port number; null if missing
path: path; / if missing
query: query parameters, returned in the order they appear as [[key, value], ...]
fragment: the part after #; null if missing
You also need to implement unquote yourself to decode percent-encoded strings:
%HH represents a hexadecimal byte, e.g. %20 decodes to a space
Consecutive %HH bytes should be decoded as UTF-8
Invalid percent encodings remain unchanged, e.g. %ZZ, %, %2
+ should not be converted to a space in this problem
Simplifications
You do not need to support:
IPv6 hosts
username/password, e.g. user:pass@host
relative-path URLs, e.g. /abc
Input Format
The first line contains an integer T, the number of URLs.
The next T lines each contain one URL.
Output Format
For each URL, output one JSON line with fixed fields:
{"scheme":...,"host":...,"port":...,"path":...,"query":...,"fragment":...}
Constraints
1 <= T <= 1000
1 <= len(url) <= 5000
The total length of all URLs is at most 200000
Example
Input:
1
https://www.airbnb.com:443/rooms/123?guest=2&city=New%20York#photos
Output:
{"scheme":"https","host":"www.airbnb.com","port":443,"path":"/rooms/123","query":[["guest","2"],["city","New York"]],"fragment":"photos"}
Example
Input
1
https://www.airbnb.com:443/rooms/123?guest=2&city=New%20York#photos
Output
{"scheme":"https","host":"www.airbnb.com","port":443,"path":"/rooms/123","query":[["guest","2"],["city","New York"]],"fragment":"photos"}