Class: Hobix::WebApp::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/hobix/webapp/cli.rb

Instance Method Summary collapse

Instance Method Details

#run_cliObject

CLI (command line interface)



112
113
114
115
116
117
118
119
120
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
# File 'lib/hobix/webapp/cli.rb', line 112

def run_cli
  opt_output = '-'
  opt_cern_meta = false
  opt_server_name = 'localhost'
  opt_server_port = 80
  opt_script_name = "/#{File.basename($0)}"
  opt_remote_addr = '127.0.0.1'
  opt_headers = []
  ARGV.options {|q|
    q.banner = "#{File.basename $0} [options] [/path_info] [?query_string]"
    q.def_option('-h', '--help', 'show this message') { puts q; exit(0) }
    q.def_option('-o FILE', '--output=FILE', 'set output file') {|arg| opt_output = arg.untaint }
    q.def_option('--cern-meta', 'output header as CERN httpd metafile') { opt_cern_meta = true }
    q.def_option('--server-name=STRING', 'set server name') {|arg| opt_server_name = arg }
    q.def_option('--server-port=INTEGER', 'set server port number') {|arg| opt_server_port = arg.to_i }
    q.def_option('--script-name=STRING', 'set script name') {|arg| opt_script_name = arg }
    q.def_option('--remote-addr=STRING', 'set remote IP address') {|arg| opt_remote_addr = arg }
    q.def_option('--header=NAME:BODY', 'set additional request header') {|arg| opt_headers << arg.split(/:/, 2) }
    q.parse!
  }
  if path_info = ARGV.shift
    if %r{\A/} !~ path_info
      ARGV.unshift path_info
      path_info = nil
    end
  end
  if query_string = ARGV.shift
    if %r{\A\?} !~ query_string
      ARGV.unshift query_string
      query_string = nil
    end
  end
  if !ARGV.empty?
    raise "extra arguments: #{ARGV.inspect[1..-2]}"
  end
  path_info ||= ''
  query_string ||= ''
  setup_request = lambda {|req|
    req.make_request_header_from_cgi_env({
      'REQUEST_METHOD' => 'GET',
      'SERVER_NAME' => opt_server_name,
      'SERVER_PORT' => opt_server_port,
      'SCRIPT_NAME' => opt_script_name,
      'PATH_INFO' => path_info,
      'QUERY_STRING' => query_string,
      'SERVER_PROTOCOL' => 'HTTP/1.0',
      'REMOTE_ADDR' => opt_remote_addr,
      'CONTENT_TYPE' => ''
    })
    opt_headers.each {|name, body|
      req.header_object.add name, body
    }
  }
  output_response = lambda {|res|
    if opt_output == '-'
      res.output_cgi_status_field($stdout)
      res.output_message($stdout)
    else
      if opt_cern_meta
        dir = "#{File.dirname(opt_output)}/.web"
        begin
          Dir.mkdir dir
        rescue Errno::EEXIST
        end
        open("#{dir}/#{File.basename(opt_output)}.meta", 'w') {|f|
          #res.output_cgi_status_field(f)
          res.output_header(f)
        }
        open(opt_output, 'w') {|f|
          res.output_body(f)
        }
      else
        open(opt_output, 'w') {|f|
          res.output_cgi_status_field(f)
          res.output_message(f)
        }
      end
    end
  }
  primitive_run(setup_request, output_response)
end