yn_server 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 64ae4767674b43b6e744f36a98a9df8aa2324a46
4
+ data.tar.gz: 3da7d3da008126b630b5679af2c6d04e2a726210
5
+ SHA512:
6
+ metadata.gz: 19e6199980d4f30b79a474e397517c48fd49d5ebc3c0aaeca6e3fde71ab35d93109b23d75436cc48587954fcd735470fbe6d8a1fdd5bf0c75b7db3138d499edc
7
+ data.tar.gz: 49ccd38963dd47b94b44cce822328b011b94ef5484c60f99bc58e0ddad9e863a34ae27b070ab7d400589b5bc63841059424350873d210957ccb066e58b70e7e5
@@ -0,0 +1,65 @@
1
+
2
+ require 'yn_socket_queue'
3
+ require 'yn_request'
4
+ require 'yn_task'
5
+ require 'yn_route_util'
6
+
7
+ # 处理http请求
8
+ # create by yan
9
+ class YNHandleRequest
10
+
11
+ def initialize(socket_queue)
12
+ @socket_queue=socket_queue
13
+ end
14
+
15
+ def handle
16
+ loop do
17
+ begin
18
+ client = @socket_queue.take
19
+ puts "-----------------------------------"
20
+ _method,path = client.gets.split
21
+ puts "url: #{path}"
22
+ puts "method: #{_method}"
23
+ # if (path=~/.+\.(png|ico|gif)/) != nil
24
+ # client.close
25
+ # next
26
+ # end
27
+ headers={}
28
+ while line = client.gets.split(' ',2)
29
+ break if line[0]==""
30
+ headers[line[0].chop] = line[1].strip
31
+ end
32
+ data = ""
33
+ servlet_url = ""
34
+ if _method.upcase == 'POST'
35
+ data = client.read(headers["Content-Length"].to_i)
36
+ servlet_url = path
37
+ elsif _method.upcase == 'GET'
38
+ if path.include? '?'
39
+ # 带参数
40
+ data = path[(path.index('?')+1)..path.length]
41
+ servlet_url = path[0...path.index('?')]
42
+ else
43
+ data = ""
44
+ servlet_url = path
45
+ end
46
+ end
47
+ request = YNRequest.new(data)
48
+ puts "parameter: #{request.hash}"
49
+ util = YNRouteUtil.new
50
+ route = util.get_method(servlet_url)
51
+ task = YNTask.new(request)
52
+ route = "default" if route == nil || route.empty?
53
+ puts "route: #{route}"
54
+ _result = task.send(route) #动态执行方法
55
+ client.write(_result)
56
+
57
+ rescue Exception => e
58
+ puts e.send(:caller)
59
+ ensure
60
+ client.close
61
+ end
62
+
63
+ end
64
+ end
65
+ end
data/lib/yn_http.rb ADDED
@@ -0,0 +1,76 @@
1
+ class YNHttp
2
+
3
+ def initialize(_status=200,_server="Apache-Coyote/1.1",_pragma="no-cache",_control="no-cache",_content_type="text/json",_charset="UTF-8",_body="")
4
+ @status = _status
5
+ @server = _server
6
+ @pragma = _pragma
7
+ @cache_control = _control
8
+ @content_type = _content_type
9
+ @charset = _charset
10
+ @body = _body
11
+ @content_length = _body.length
12
+ end
13
+
14
+ @@status_hash={
15
+ 100 => "CONTINUE",
16
+ 200 => "OK",
17
+ 201 => "CREATED",
18
+ 202 => "ACCEPTED",
19
+ 204 => "NO CONTENT",
20
+ 302 => "MOVED TEMPORARILY",
21
+ 400 => "BAD REQUEST",
22
+ 401 => "UNAUTHORIZED",
23
+ 402 => "PAYMENT REQUIRED",
24
+ 403 => "FORBIDDEN",
25
+ 404 => "NOT FOUND",
26
+ 408 => "REQUEST TIMEOUT",
27
+ 409 => "CONFLICT",
28
+ 410 => "GONE",
29
+ 500 => "INTERNAL SERVER ERROR"
30
+ }
31
+
32
+ def status=(_status)
33
+ @status=_status
34
+ end
35
+
36
+ def server=(_server)
37
+ @server=_server
38
+ end
39
+
40
+ def pragma=(_pragma)
41
+ @pragma=_pragma
42
+ end
43
+
44
+ def cache_control=(_control)
45
+ @cache_control=_control
46
+ end
47
+
48
+ def content_type=(_content_type)
49
+ @content_type=_content_type
50
+ end
51
+
52
+ def charset=(_charset)
53
+ @charset=_charset
54
+ end
55
+
56
+ def body=(result)
57
+ @content_length=result.size
58
+ @body=result
59
+ end
60
+
61
+ def response
62
+ "HTTP/1.1 #{@status} #{@@status_hash[@status]}\r\n" +
63
+ "Server:#{@server}\r\n" +
64
+ "Pragma:#{@pragma}\r\n" +
65
+ "Cache-Control:#{@cache_control}\r\n" +
66
+ "Content-Type:#{@content_type};charset=#{@charset}\r\n" +
67
+ "Content-Length:#{@content_length}\r\n" +
68
+ "\r\n" +
69
+ "#{@body}"
70
+ end
71
+ end
72
+
73
+
74
+
75
+
76
+
data/lib/yn_request.rb ADDED
@@ -0,0 +1,30 @@
1
+
2
+ class YNRequest
3
+
4
+ include Enumerable
5
+
6
+ attr_reader :hash
7
+
8
+ def initialize(content="")
9
+ @hash = Hash.new
10
+ if content != ""
11
+ _arr = []
12
+ _arr = content.include?("&") ? content.split("&") : _arr.push(content)
13
+ _arr.each do |e|
14
+ __arr = e.split("=")
15
+ @hash[__arr[0].to_sym]=__arr[1]
16
+ end
17
+ end
18
+ end
19
+
20
+ def get(key)
21
+ @hash[key.to_sym]
22
+ end
23
+
24
+ def each
25
+ raise 'please provide a block!' unless block_given?
26
+ @hash.each do |e|
27
+ yield e
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ require 'yaml'
2
+
3
+ # 配置请求路由
4
+ # create by yan
5
+
6
+ class YNRouteUtil
7
+
8
+ include Enumerable
9
+
10
+ @@route_hash = {
11
+ "/RubyServer/hello" => "say_hello",
12
+ "/RubyServer/json" => "test_json",
13
+ "/RubyServer/none" => "none_method"
14
+ }
15
+
16
+
17
+ # def initialize
18
+ # @route_hash = Hash.new
19
+ # puts "route util initialize"
20
+ # _arr = YAML.load_file('route.properties').split(" ")
21
+ # puts "route_file: #{_arr}"
22
+ # _arr.each do |e|
23
+ # __arr = e.split("=")
24
+ # @route_hash[__arr[0]] = __arr[1]
25
+ # end
26
+
27
+ # end
28
+
29
+ def get_method(route)
30
+ @@route_hash[route]
31
+ end
32
+
33
+ def each
34
+ raise 'please provide a block!' unless block_given?
35
+ @@route_hash.each do |e|
36
+ yield e
37
+ end
38
+ end
39
+
40
+ end
data/lib/yn_server.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'yn_socket_server'
2
+ require 'yn_socket_queue'
3
+ require 'yn_handle_request'
4
+ require 'yaml'
5
+
6
+ class YNServer
7
+ def self.start(port=2000)
8
+ queue = YNSocketQueue.new
9
+ thread2 = Thread.new do
10
+ socket_server = YNSocketServer.new(port,queue)
11
+ socket_server.start_server
12
+ end
13
+ thread = Thread.new do
14
+ handle_request = YNHandleRequest.new(queue)
15
+ handle_request.handle
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,27 @@
1
+ ################################
2
+ # base info
3
+ # 用来存放所有Socket请求
4
+ # created by yan on 17/3/20
5
+ # blog mia2002.cn
6
+ ################################
7
+
8
+ class YNSocketQueue
9
+
10
+ def initialize()
11
+ @queue=Queue.new
12
+ end
13
+
14
+ def push(socket)
15
+ raise "Illegal Argument, must be a TCPSocket Object!!" unless socket.is_a? TCPSocket
16
+ if socket != nil
17
+ @queue << socket
18
+ end
19
+ end
20
+
21
+ def take
22
+ @queue.pop
23
+ end
24
+ end
25
+
26
+
27
+
@@ -0,0 +1,27 @@
1
+ require 'socket'
2
+ require 'yn_socket_queue'
3
+ require 'yn_handle_request'
4
+
5
+ class YNSocketServer
6
+
7
+ # 初始化
8
+ # port 端口号
9
+ def initialize(port,queue)
10
+ @port = port
11
+ @queue = queue
12
+ end
13
+
14
+ def start_server
15
+ begin
16
+ @server=TCPServer.open(@port)
17
+ puts "start successfully!!"
18
+ loop{
19
+ @client=@server.accept
20
+ @queue.push(@client)
21
+ }
22
+ rescue Exception => e
23
+ puts e.send(:caller)
24
+ end
25
+
26
+ end
27
+ end
data/lib/yn_task.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'json'
2
+ require 'yn_http'
3
+ require 'yn_request'
4
+
5
+ class YNTask
6
+ def initialize(request)
7
+ @request = request
8
+ end
9
+
10
+ def method_missing(method_name)
11
+ puts "#{method_name} not found in YNTask,please check yn_route_uril.rb"
12
+ default
13
+ end
14
+
15
+ # route: /RubyServer/hello
16
+ def say_hello
17
+ begin
18
+ http = YNHttp.new
19
+ http.content_type = "text/html"
20
+ _param = ""
21
+ @request.each do |e|
22
+ _param+="#{e[0]}=#{e[1]}<br>"
23
+ end
24
+
25
+ http.body = "<html><head><title>Hello to Ruby Server</title></html><body><h1>Hi,welcome to Yan's ruby server!</h1><p>the request param:<br>#{_param}</p></body></html>"
26
+ # return the response result
27
+ http.response
28
+ rescue Exception => e
29
+ puts e.send(:caller)
30
+ end
31
+
32
+ end
33
+
34
+ # route: /RubyServer/json
35
+ def test_json
36
+ http = YNHttp.new
37
+ http.content_type = "text/json"
38
+ result = JSON.generate(@request.hash)
39
+ http.body = result
40
+ http.response
41
+ end
42
+
43
+ def default
44
+ http = YNHttp.new
45
+ http.content_type = "text/html"
46
+ http.status = 404
47
+ http.body = "<html><head><title>Welcome</title></html><body><h1>Welcome to Yan's ruby server!</h1><p><h3>404 Not Found</h3></p></body></html>"
48
+ http.response
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yn_server
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Yan Ng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple socket server gem
14
+ email: yan@yerl.cn
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/yn_handle_request.rb
20
+ - lib/yn_http.rb
21
+ - lib/yn_server.rb
22
+ - lib/yn_request.rb
23
+ - lib/yn_route_util.rb
24
+ - lib/yn_socket_queue.rb
25
+ - lib/yn_socket_server.rb
26
+ - lib/yn_task.rb
27
+ homepage: http://rubygems.org/gems/yn_server
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.0.14.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Ruby Server!
51
+ test_files: []