httprecognizer 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d9924478ac865e8df2f2d5428e971ac75b7ba7a
4
+ data.tar.gz: 8a1e8f00ee817b10742b6ee833126939a14f5e84
5
+ SHA512:
6
+ metadata.gz: 25a54468702ea1d913b0fd8ec237c8f0102a43005027f6b4e3904790b7f2fb987f60f3c9a3acfc0f754c24ba512e0d61b264e5bcd4c02a318d9352ae436b5d20
7
+ data.tar.gz: a9864fe1ff6363d6443050e1739f86d839784fbc194014512750b0ab3d4f31515e52b10e9c3c23f2d00b22e5e985338ed390ddebb3d34daaa5cab7147e7202be
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at wyhaines@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in http-no-a-parser.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kirk Haines
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # HttpRecognizer
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'httprecognizer'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install httprecognizer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[wyhaines]/httprecognizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
37
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "httprecognizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'httprecognizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "httprecognizer"
8
+ spec.version = HttpRecognizer::VERSION
9
+ spec.authors = ["Kirk Haines"]
10
+ spec.email = ["wyhaines@gmail.com"]
11
+
12
+ spec.summary = %q{A very simple implementation of an HTTP Recognizer. Less than a parser, but more than nothing.}
13
+ spec.description = %q{The HttpRecognizer receives streamed HTTP, and when it accumulates enough to identify that it's seen an HTTP header, it attempts to extract some basic information from it. This is not a parser, and it is woefully incomplete, but for many tasks it understands just enough HTTP to be useful.}
14
+ spec.homepage = "http://github.com/wyhaines/httprecognizer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.require_paths = ["lib"]
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_runtime_dependency 'deque', '~> 1.0'
26
+ end
@@ -0,0 +1,212 @@
1
+ require 'httprecognizer/constants'
2
+ require 'httprecognizer/version'
3
+ require 'deque'
4
+
5
+ # Parent expected to provide following API:
6
+ #
7
+ # default_name
8
+ # known_name?
9
+ # keepalive?
10
+ # now
11
+ # logger
12
+ # add_client
13
+ # remove_client
14
+ #
15
+ # This module implements the HTTP handling code. I call it a recognizer,
16
+ # and not a parser because it does not parse HTTP. It is much simpler than
17
+ # that, being designed only to recognize certain useful bits very quickly.
18
+
19
+ class HttpRecognizer
20
+ class NotImplemented < Exception; end
21
+ class Http400Error < Exception; end
22
+ class Http404Error < Exception; end
23
+
24
+ attr_accessor :create_time, :last_action_time, :uri, :unparsed_uri, :associate, :name, :redeployable, :data_pos, :data_len, :peer_ip, :connection_header, :keepalive, :header_data
25
+
26
+ # Initialize the @data array, which is the temporary storage for blocks
27
+ # of data received from the web browser client, then invoke the superclass
28
+ # initialization.
29
+
30
+ def initialize webserver=nil
31
+ @data = Deque.new
32
+ @data_pos = 0
33
+ @connection_header = C_empty
34
+ @header_missing_pieces = @name = @uri = @unparsed_uri = @http_version = @request_method = @none_match = @done_parsing = @header_data = nil
35
+ @keepalive = true
36
+ @webserver = webserver
37
+ end
38
+
39
+ def done_parsing?
40
+ @done_parsing
41
+ end
42
+
43
+ def reset_state
44
+ @data.clear
45
+ @data_pos = 0
46
+ @connection_header = C_empty
47
+ @header_missing_pieces = @name = @uri = @unparsed_uri = @http_version = @request_method = @none_match = @done_parsing = @header_data = nil
48
+ @keepalive = true
49
+ end
50
+
51
+ # States:
52
+ # uri
53
+ # name
54
+ # \r\n\r\n
55
+ # If-None-Match
56
+ # Done Parsing
57
+ def receive_data data
58
+ if @done_parsing
59
+ @data.unshift data
60
+ push
61
+ else
62
+ if @header_missing_pieces
63
+ # Hopefully this doesn't happen often.
64
+ d = @data.to_s << data
65
+ @header_missing_pieces = false
66
+ else
67
+ d = data
68
+ end
69
+ unless @uri
70
+ # It's amazing how, when writing the code, the brain can be in a zone
71
+ # where line noise like this regexp makes perfect sense, and is clear
72
+ # as day; one looks at it and it reads like a sentence. Then, one
73
+ # comes back to it later, and looks at it when the brain is in a
74
+ # different zone, and 'lo! It looks like line noise again.
75
+ #
76
+ # data =~ /^(\w+) +(?:\w+:\/\/([^\/]+))?([^ \?]+)\S* +HTTP\/(\d\.\d)/
77
+ #
78
+ # In case it looks like line noise to you, dear reader, too:
79
+ #
80
+ # 1) Match and save the first set of word characters.
81
+ #
82
+ # Followed by one or more spaces.
83
+ #
84
+ # Match but do not save the word characters followed by ://
85
+ #
86
+ # 2) Match and save one or more characters that are not a slash
87
+ #
88
+ # And allow this whole thing to match 1 or 0 times.
89
+ #
90
+ # 3) Match and save one or more characters that are not a question
91
+ # mark or a space.
92
+ #
93
+ # Match zero or more non-whitespace characters, followed by one
94
+ # or more spaces, followed by "HTTP/".
95
+ #
96
+ # 4) Match and save a digit dot digit.
97
+ #
98
+ # Thus, this pattern will match both the standard:
99
+ # GET /bar HTTP/1.1
100
+ # style request, as well as the valid (for a proxy) but less common:
101
+ # GET http://foo/bar HTTP/1.0
102
+ #
103
+ # If the match fails, then this is a bad request, and an appropriate
104
+ # response will be returned.
105
+ #
106
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec5.1.2
107
+ #
108
+ if d =~ /^(\w+) +(?:\w+:\/\/([^ \/]+))?(([^ \?\#]*)\S*) +HTTP\/(\d\.\d)/
109
+ @request_method = $1
110
+ @unparsed_uri = $3
111
+ @uri = $4
112
+ @http_version = $5
113
+ if $2
114
+ @name = $2.intern
115
+ @uri = C_slash if @uri.empty?
116
+ # Rewrite the request to get rid of the http://foo portion.
117
+
118
+ d.sub!(/^\w+ +\w+:\/\/[^ \/]+([^ \?]*)/,"#{@request_method} #{@uri}")
119
+ end
120
+ @uri = @uri.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) {[$1.delete(C_percent)].pack('H*')} if @uri.include?(C_percent)
121
+ elsif d.include?(Crn)
122
+ raise Http400Error
123
+ return
124
+ end
125
+ end
126
+ unless @name
127
+ if d =~ /^Host:\s*([^\r\0:]+)\r?\n/m
128
+ @name = $1.intern
129
+ end
130
+ end
131
+ if d.include?(Crnrn)
132
+ @name = ( @webserver.respond_to?(:default_name) && @webserver.respond_to?(:known_name?) ) ?
133
+ ( @webserver.known_name?(@name) ? @name : @webserver.default_name ) :
134
+ @name
135
+ if d =~ /If-None-Match: *([^\r]+)/
136
+ @none_match = $1
137
+ end
138
+ @header_data = d.scan(/Cookie:.*/).collect {|c| c =~ /: ([^\r]*)/; $1}
139
+ @done_parsing = true
140
+
141
+ # Keep-Alive works differently on HTTP 1.0 versus HTTP 1.1
142
+ # HTTP 1.0 was not written to support Keep-Alive initially; it was
143
+ # bolted on. Thus, for an HTTP 1.0 client to indicate that it
144
+ # wants to initiate a Keep-Alive session, it must send a header:
145
+ #
146
+ # Connection: Keep-Alive
147
+ #
148
+ # Then, when the server sends the response, it must likewise add:
149
+ #
150
+ # Connection: Keep-Alive
151
+ #
152
+ # to the response.
153
+ #
154
+ # For HTTP 1.1, Keep-Alive is assumed. If a client does not want
155
+ # Keep-Alive, then it must send the following header:
156
+ #
157
+ # Connection: close
158
+ #
159
+ # Likewise, if the server does not want to keep the connection
160
+ # alive, it must send the same header:
161
+ #
162
+ # Connection: close
163
+ #
164
+ # to the client.
165
+
166
+ if @name
167
+ unless @webserver.respond_to?(:keepalive) && @webserver.keepalive(@name) == false
168
+ if @http_version == C1_0
169
+ if data =~ /Connection: Keep-Alive/i
170
+ # Nonstandard HTTP 1.0 situation; apply keepalive header.
171
+ @connection_header = CConnection_KeepAlive
172
+ else
173
+ # Standard HTTP 1.0 situation; connection will be closed.
174
+ @keepalive = false
175
+ @connection_header = CConnection_close
176
+ end
177
+ else # The connection is an HTTP 1.1 connection.
178
+ if data =~ /Connection: [Cc]lose/
179
+ # Nonstandard HTTP 1.1 situation; connection will be closed.
180
+ @keepalive = false
181
+ end
182
+ end
183
+ end
184
+
185
+ @webserver.add_client(self,@data,data) if @webserver.respond_to?(:add_client)
186
+ else
187
+ raise Http404Error
188
+ end
189
+ else
190
+ @data.push data
191
+ @header_missing_pieces = true
192
+ end
193
+ end
194
+ end
195
+
196
+ # The push method pushes data from the HttpRecognizer to whatever
197
+ # entity is responsible for handling it. You MUST override this with
198
+ # something useful.
199
+
200
+ def push
201
+ raise NotImplemented
202
+ end
203
+
204
+ def request_method; @request_method; end
205
+ def http_version; @http_version; end
206
+ def none_match; @none_match; end
207
+
208
+ def setup_for_redeployment
209
+ @data_pos = 0
210
+ end
211
+
212
+ end
@@ -0,0 +1,14 @@
1
+ class HttpRecognizer
2
+ C_slash = '/'.freeze
3
+ C1_0 = '1.0'.freeze
4
+ C1_1 = '1.1'.freeze
5
+ CConnection_close = "Connection: close\r\n".freeze
6
+ CConnection_KeepAlive = "Connection: Keep-Alive\r\n".freeze
7
+ Crn = "\r\n".freeze
8
+ Crnrn = "\r\n\r\n".freeze
9
+ C_empty = ''.freeze
10
+ C_percent = '%'.freeze
11
+ Cunknown_host = 'unknown host'.freeze
12
+ C404Header = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n"
13
+ C400Header = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n"
14
+ end
@@ -0,0 +1,3 @@
1
+ class HttpRecognizer
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httprecognizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Haines
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: deque
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: The HttpRecognizer receives streamed HTTP, and when it accumulates enough
70
+ to identify that it's seen an HTTP header, it attempts to extract some basic information
71
+ from it. This is not a parser, and it is woefully incomplete, but for many tasks
72
+ it understands just enough HTTP to be useful.
73
+ email:
74
+ - wyhaines@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - httprecognizer.gemspec
87
+ - lib/httprecognizer.rb
88
+ - lib/httprecognizer/constants.rb
89
+ - lib/httprecognizer/version.rb
90
+ homepage: http://github.com/wyhaines/httprecognizer
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A very simple implementation of an HTTP Recognizer. Less than a parser, but
114
+ more than nothing.
115
+ test_files: []