12module remote34import net.http5import net.urllib67pub struct Web {8pub mut:9 base string10 11 on_body fn (received u64) = unsafe { nil }12}1314pub fn new_web(base string) !Web {15 return Web{ base: kepr_host(base)! }16}17181920pub fn kepr_host(value string) !string {21 for letter in value {22 if letter <= ` ` {23 return error('"${value}" is not a Kepr address — try kepr.uk')24 }25 }26 text := if value.starts_with('http://') || value.starts_with('https://') {27 value28 } else {29 'https://${value}'30 }31 parsed := urllib.parse(text) or { return error('"${value}" is not a Kepr address — try kepr.uk') }32 if parsed.scheme !in ['http', 'https'] || parsed.host == '' || parsed.path.trim_right('/') != '' || parsed.raw_query != '' || parsed.fragment != '' {33 return error('"${value}" is not a Kepr address — try kepr.uk')34 }35 if text.all_after('//').contains('@') {36 return error('"${value}" is not a Kepr address — try kepr.uk')37 }38 39 40 if parsed.scheme == 'http' && parsed.hostname().to_lower() !in ['127.0.0.1', 'localhost', '::1'] {41 return error('"${value}" needs https — plain http is only for a Kepr on this machine')42 }43 44 return '${parsed.scheme}://${parsed.host.to_lower()}'45}4647pub fn (mut w Web) get(key string) !Object {48 response := http.fetch(49 url: w.url(key)50 method: .get51 enable_http2: false52 allow_redirect: false53 )!54 if response.status_code == 404 {55 return Object{}56 }57 if response.status_code < 200 || response.status_code >= 300 {58 return error('web GET ${key} failed: ${response.status_code}')59 }60 return Object{61 found: true62 body: response.body.bytes()63 tag: response.header.get_custom('etag', http.HeaderQueryConfig{}) or { '' }64 }65}66676869pub fn (mut w Web) stream(key string, sink fn (piece []u8) !) !bool {70 on_body := w.on_body71 response := http.fetch(72 url: w.url(key)73 method: .get74 enable_http2: false75 allow_redirect: false76 77 stop_copying_limit: 64 * 102478 on_progress_body: fn [sink, on_body] (_ &http.Request, piece []u8, received u64, _ u64, status int) ! {79 80 81 if status < 200 || status >= 300 || received == 0 {82 return83 }84 sink(piece)!85 if on_body != unsafe { nil } && (received - u64(piece.len)) >> 20 != received >> 20 {86 on_body(received)87 }88 }89 )!90 if response.status_code == 404 {91 return false92 }93 if response.status_code < 200 || response.status_code >= 300 {94 return error('web GET ${key} failed: ${response.status_code}')95 }96 return true97}9899pub fn (mut w Web) list(prefix string) ![]string {100 response := http.fetch(101 url: w.url(prefix)102 method: .get103 enable_http2: false104 allow_redirect: false105 )!106 if response.status_code == 404 {107 return []string{}108 }109 if response.status_code < 200 || response.status_code >= 300 {110 return error('web GET ${prefix} failed: ${response.status_code}')111 }112 mut result := []string{}113 for id in response.body.split_into_lines() {114 if id.trim_space() != '' { result << '${prefix}/${id.trim_space()}' }115 }116 return result117}118119pub fn (mut w Web) put_if_absent(key string, body []u8) !bool {120 return error('${w.repo(key)} is read-only.')121}122123pub fn (mut w Web) put_if_match(key string, body []u8, tag string) !bool {124 return error('${w.repo(key)} is read-only.')125}126127pub fn (mut w Web) delete(key string) ! {128 return error('${w.repo(key)} is read-only.')129}130131fn (w &Web) url(key string) string {132 parts := key.split('/')133 return '${w.base}/${parts[0]}/koh/${parts[1..].join('/')}'134}135136fn (w &Web) repo(key string) string {137 return '${w.base}/${key.all_before('/')}'138}