Top Level Namespace
Defined Under Namespace
Modules: Gorp Classes: Fixnum, HTMLRunner, Proc, String
Constant Summary collapse
- FILE_SEPARATOR =
'\\'
- DEV_NULL =
'NUL'
Instance Method Summary collapse
- #edit(filename, tag = nil, &block) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #post(path, form, options = {}) ⇒ Object
- #read(name) ⇒ Object
- #snap(response, form = nil) ⇒ Object
- #update_cookies(uri, response) ⇒ Object
Instance Method Details
#edit(filename, tag = nil, &block) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/gorp/edit.rb', line 163 def edit filename, tag=nil, &block filename = Dir[filename].first || filename if filename.include? '*' $x.pre "edit #{filename.gsub('/',FILE_SEPARATOR)}", :class=>'stdin' stale = File.mtime(filename) rescue Time.now-2 data = open(filename) {|file| file.read} rescue '' before = data.split("\n") begin data.extend Gorp::StringEditingFunctions data.instance_exec(data, &block) if block_given? # write the file, ensuring that the file timestamp changed while true open(filename,'w') {|file| file.write data} $lastmod = File.mtime(filename) break if $lastmod > stale and Time.now >= $lastmod sleep 0.1 end rescue Exception => e $x.pre :class => 'traceback' do STDERR.puts e.inspect $x.text! "#{e.inspect}\n" e.backtrace.each {|line| $x.text! " #{line}\n"} end tag = nil ensure log :edit, filename.gsub('/',FILE_SEPARATOR) include = tag.nil? highlight = false data.split("\n").each do |line| if line =~ /START:(\w+)/ include = true if $1 == tag elsif line =~ /END:(\w+)/ include = false if $1 == tag elsif line =~ /START_HIGHLIGHT/ highlight = true elsif line =~ /END_HIGHLIGHT/ highlight = false elsif include if highlight or ! before.include?(line) outclass='hilight' else outclass='stdout' end if line.empty? $x.pre ' ', :class=>outclass else $x.pre line, :class=>outclass end end end end end |
#get(path, options = {}) ⇒ Object
117 118 119 |
# File 'lib/gorp/net.rb', line 117 def get path, ={} post path, nil, end |
#post(path, form, options = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/gorp/net.rb', line 121 def post path, form, ={} $lastmod ||= Time.now log :get, path unless form $x.pre "get #{path}", :class=>'stdin' unless [:snapget] == false if path.include? ':' host, port, path = URI.parse(path).select(:host, :port, :path) else host, port = 'localhost', $PORT end Net::HTTP.start(host, port) do |http| accept = [:accept] || 'text/html' accept = 'application/atom+xml' if path =~ /\.atom$/ accept = 'application/json' if path =~ /\.json$/ accept = 'application/xml' if path =~ /\.xml$/ uri = URI.parse("http://#{host}:#{port}/#{path}") get = Net::HTTP::Get.new(path, 'Accept' => accept) get.basic_auth *[:auth] if [:auth] get['Cookie'] = HTTP::Cookie.($COOKIEJAR.(uri)) response = http.request(get) snap response, form unless [:snapget] == false uri, response if form body = xhtmlparse(response.body).at('//body') rescue nil body = xhtmlparse(response.body).root unless body rescue nil return unless body xforms = body.search('//form') # find matching button by action xform = xforms.find do |element| next unless element['action'].include?('?') query = CGI.parse(URI.parse(element['action']).query) query.all? {|key,values| values.include?(form[key].to_s)} end # find matching button by input names xform ||= xforms.find do |element| form.all? do |name, value| element.search('.//input | .//textarea | ..//select').any? do |input| input['name']==name.to_s end end end # find matching submit button xform ||= xforms.find do |element| form.all? do |name, value| element.search('.//input[@type="submit"]').any? do |input| input['value']==form['submit'] end end end # match based on action itself xform ||= xforms.find do |element| action=CGI.unescape(element['action']) form.all? {|name, value| action.include? "#{name}=#{value}"} end # look for a commit button xform ||= xforms.find {|element| element.at('.//input[@name="commit"]')} return unless xform path = xform['action'] unless xform['action'].empty? path = CGI::unescapeHTML(path) $x.pre "post #{path}", :class=>'stdin' $x.ul do form.each do |name, value| $x.li "#{name} => #{value}" unless $CookieDebug end xform.search('.//input[@type="hidden"]').each do |element| if $CookieDebug $x.li "#{element['name']} => #{element['value']}" end form[element['name']] ||= element['value'] end if $CookieDebug uri = URI.parse("http://#{host}:#{port}/#{path}") $COOKIEJAR.(uri).each do || $x.li do $x.b {$x.em '[cookie]'} $x.text! .to_s end end end end log :post, path uri = URI.parse("http://#{host}:#{port}/#{path}") post = Net::HTTP::Post.new(path) post.set_form_data form post['Content-Type'] = 'application/x-www-form-urlencoded' post['Cookie'] = HTTP::Cookie.($COOKIEJAR.(uri)) response=http.request(post) snap response uri, response end if response.code == '302' path = response['Location'] uri = URI.parse("http://#{host}:#{port}/#{path}") $x.pre "get #{path}", :class=>'stdin' get = Net::HTTP::Get.new(path, 'Accept' => accept) get['Cookie'] = HTTP::Cookie.($COOKIEJAR.(uri)) response = http.request(get) snap response uri, response end end rescue Timeout::Error Gorp::Commands.stop_server(false, 9) ensure while true open('tmp/lastmod', 'w') {|file| file.write 'data'} break if File.mtime('tmp/lastmod') > $lastmod sleep 0.1 end File.unlink('tmp/lastmod') end |
#read(name) ⇒ Object
222 223 224 |
# File 'lib/gorp/edit.rb', line 222 def read name open(File.join($DATA, name)) {|file| file.read} end |
#snap(response, form = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/gorp/net.rb', line 25 def snap response, form=nil if response.code >= '400' $x.p "HTTP Response Code: #{response.code}", :class => 'traceback' end if response.content_type == 'text/plain' or response.content_type =~ /xml/ $x.div :class => 'body' do response.body.split("\n").each do |line| $x.pre line.chomp, :class=>'stdout' end end return end if response.body =~ /<body/ body = response.body elsif response.body =~ /<BODY/ body = response.body.gsub(/<\/?\w+/) {|tag| tag.downcase} body.gsub! '<hr>', '<hr/>' else body = "<body>#{response.body}</body>" end begin doc = xhtmlparse(body) rescue body.split("\n").each {|line| $x.pre line.chomp, :class=>'hilight'} raise end title = doc.at('html/head/title').text rescue '' body = doc.at('//body') doc.search('//link[@rel="stylesheet"]').each do |sheet| body.children.first.add_previous_sibling(sheet) end # ensure that textareas don't use the self-closing syntax body.search('//textarea').each do |element| element.content='' end if form body.search('//input[@name]').each do |input| input['value'] ||= form[input['name']].to_s end body.search('//textarea[@name]').each do |textarea| textarea.content = form[textarea['name']].to_s if textarea.text.empty? end end %w{ a[@href] form[@action] }.each do |xpath| name = xpath[/@(\w+)/,1] body.search("//#{xpath}").each do |element| if element[name] =~ /^http:\/\// element[name] = element[name].sub('127.0.0.1', 'localhost') else element[name]=URI.join("http://localhost:#{$PORT}/", element[name]).to_s end end end %w{ img[@src] }.each do |xpath| name = xpath[/@(\w+)/,1] body.search("//#{xpath}").each do |element| if element[name][0] == ?/ element[name] = 'data' + element[name] end end end attrs = {:class => 'body', :title => title} attrs[:class] = 'traceback' if response.code == '500' attrs[:id] = body['id'] if body['id'] attrs[:class] += ' ' +body['class'] if body['class'] $x.div(attrs) do html_voids = %w(area base br col command embed hr img input keygen link meta param source) body.children.each do |child| next if child.instance_of?(Comment) child.search("//*").each do |element| next if html_voids.include? element.name if element.children.empty? and element.text.empty? element.add_child(Nokogiri::XML::Text.new('', element.document)) end end $x << child.to_xml end end $x.div '', :style => "clear: both" end |
#update_cookies(uri, response) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gorp/net.rb', line 8 def (uri, response) fields = response.get_fields('Set-Cookie') return unless fields fields.each {|value| $COOKIEJAR.parse(value, uri)} if $CookieDebug $x.ul do fields.each do |value| $x.li do $x.b {$x.em '[cookie]'} $x.text! value.to_s end end end end end |