Debian Codegraph @

lib/ninix/dll/httpc.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # httpc.rb - a HTTPC compatible Saori module for ninix
4 # Copyright (C) 2011-2019 by Shyouzou Sugitani <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License (version 2) as
8 # published by the Free Software Foundation. It is distributed in the
9 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the GNU General Public License for more details.
12 #
13
14 require "uri"
15 require "open-uri"
16
17 require "gtk4"
18
19 require_relative "../dll"
20
21
22 module Httpc
23
24 class Saori < DLL::SAORI
25
26 def initialize
27 super()
28 @__sakura = nil
29 @__bg = {}
30 end
31
32 def finalize
33 @__bg.each_key {|timeout_id| GLib::Source.remove(timeout_id) }
34 return 1
35 end
36
37 def need_ghost_backdoor(sakura)
38 @__sakura = sakura
39 end
40
41 def check_import
42 @__sakura.nil? ? 0 : 1
43 end
44
45 def get(url, start: nil, end_: nil)
46 url = URI.parse(url)
47 unless ((url.scheme == 'http' or url.scheme == 'https') and
48 #url.params.nil? and
49 url.query.nil? and
50 url.fragment.nil?)
51 return RESPONSE[400] # XXX
52 end
53 data = open(url) do |f|
54 @charset = f.charset
55 f.read()
56 end
57 unless start.nil?
58 fail "assert" if end_.nil?
59 nc = 0
60 ls = start.length
61 le = end_.length
62 result = []
63 while true
64 ns = data.index(start, nc)
65 break if ns.nil?
66 ns += ls
67 ne = data.index(end_, ns)
68 break if ne.nil?
69 nc = (ne + le)
70 result << data[ns..ne-1]
71 end
72 else
73 result = [data]
74 end
75 return result
76 end
77
78 def execute(argument)
79 return RESPONSE[400] if argument.nil?
80 bg = nil
81 @charset = nil
82 process_tag = nil
83 if argument.length >= 1
84 if argument[0] == 'bg'
85 if argument.length < 2
86 # 'bgするならIDを指定していただけませんと。'
87 return RESPONSE[400]
88 end
89 bg = argument[1]
90 argument = argument[2..-1]
91 end
92 end
93 if argument.length >= 1
94 case argument[0]
95 when 'sjis', 'utf-8', 'utf-16be', 'utf-16le'
96 @charset = argument[0]
97 argument = argument[1..-1]
98 when 'euc'
99 @charset = 'EUC-JP'
100 argument = argument[1..-1]
101 when 'jis'
102 @charset = 'ISO-2022-JP '
103 argument = argument[1..-1]
104 end
105 case argument[0]
106 when 'erase_tag'
107 process_tag = lambda {} ## FIXME: not supported yet
108 argument = argument[1..-1]
109 when 'translate_tag'
110 process_tag = lambda {} ## FIXME: not supported yet
111 argument = argument[1..-1]
112 end
113 end
114 if argument.empty?
115 ##fail "assert" unless bg.nil? and process_tag.nil?
116 return "SAORI/1.0 200 OK\r\nResult: #{@loaded} \r\n\r\n"
117 elsif argument.length > 3
118 return RESPONSE[400]
119 elsif argument.length == 2 # FIXME: not supported yet
120 return "SAORI/1.0 200 OK\r\nResult: 0\r\n\r\n"
121 else
122 if not bg.nil? # needs multi-threading?
123 timeout_id = GLib::Timeout.add(1000) { notify(bg, argument, process_tag) } # XXX
124 @__bg[timeout_id] = bg
125 return nil # "SAORI/1.0 204 No Content\r\n\r\n"
126 else
127 data = get(argument[0], :start => argument[1], :end_ => argument[2])
128 return nil if data.empty? # "SAORI/1.0 204 No Content\r\n\r\n"
129 return data if data == RESPONSE[400] # XXX
130 result = "SAORI/1.0 200 OK\r\nResult: #{data[0]}\r\n"
131 for n in 0..data.length-1
132 result = [result, "Value#{n}: #{data[n]}\r\n"].join("")
133 end
134 result += "\r\n"
135 return result.encode('Shift_JIS', :invalid => :replace, :undef => :replace)
136 end
137 end
138 end
139
140 def notify(id, argument, process_tag)
141 result = get(argument[0], :start => argument[1], :end_ => argument[2])
142 if not process_tag.nil?
143 #pass ## FIXME: not supported yet
144 end
145 @__sakura.notify_event('OnHttpcNotify', id, nil, *result)
146 end
147 end
148 end