Commit 4d4e9c9b authored by Xavier Thompson's avatar Xavier Thompson

Implement URL parser

parent 416ac9dc
from stdlib.string cimport Str
cdef cypclass URI:
Str full
Str scheme
Str authority
Str path
Str query
Str fragment
__init__(self, Str uri):
self.full = uri
self.parse(uri)
void parse(self, Str uri):
cdef int pos = 0
cdef int nxt
cdef Str COLUMN = Str(':')
cdef Str SLASH = Str('/')
cdef Str SLASHSLASH = SLASH + SLASH
cdef Str QUERY = Str('?')
cdef Str FRAGMENT = Str('#')
nxt = uri.find(COLUMN, pos)
if nxt != -1:
self.scheme = uri.substr(pos, nxt)
pos = nxt + COLUMN.__len__()
if uri.find(SLASHSLASH, pos, pos + SLASHSLASH.__len__()) != -1:
pos = nxt + SLASHSLASH.__len__()
nxt = uri.find(SLASH, pos)
if nxt == -1:
nxt = uri.find(QUERY, pos)
if nxt == -1:
nxt = uri.find(FRAGMENT, pos)
if nxt == -1:
self.authority = uri.substr(pos)
return
self.authority = uri.substr(pos, nxt)
pos = nxt
nxt = uri.find(QUERY, pos)
if nxt == -1:
nxt = uri.find(FRAGMENT, pos)
if nxt > pos:
self.path = uri.substr(pos, nxt)
elif nxt == -1:
self.path = uri.substr(pos)
return
else:
if nxt > pos:
self.path = uri.substr(pos, nxt)
pos = nxt + QUERY.__len__()
nxt = uri.find(FRAGMENT, pos)
if nxt == -1:
self.query = uri.substr(pos)
return
self.query = uri.substr(pos, nxt)
pos = nxt + FRAGMENT.__len__()
self.fragment = uri.substr(pos)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment