diamant 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a987ba57250c314c052adfa5f913be3312999154e64aa346df39e7c318c41f39
4
- data.tar.gz: 052f61456c18fdfe8241f3333e81a1c8ab85d7a8fcd8fb4f671179793e5ce1ee
3
+ metadata.gz: 72a20983fb5689076b1da91f753515fb251f96c73917628ba5d2e975d34c526c
4
+ data.tar.gz: efed078e4f3f0f2a556da98b2308fdc2d6d2af927b4e7ef61901b08490e4ef55
5
5
  SHA512:
6
- metadata.gz: 32ba003a7ce3c3f40f498ad51c7de1a4a292d1f0f13a89855c288ea08555cdfcf5d0bf6ecd39cc08d98097662675af96aa4f74c102f667e7edd19e984b7561e8
7
- data.tar.gz: 3521229067830e6f91cca8bb0732ae70a2ac9a7cc04072a11b2513b0e1e5b9494300e5f722c4bbdba25c8e651401f068ce30f240acb2b6b161b30ae1a215ea95
6
+ metadata.gz: c15a92708e8b9bf8ebc746dd850e4f500affbac1fe32e29c0ee2b73efd998b12865ba2e6f68dd130bf42f3f042eed267852ed9a545bcc15797264ccbd39edc3f
7
+ data.tar.gz: '084cca645aa707d1140a787a35a8a548a2385bb9dfc4aa25110df0f8b75875238595accf4feb21bcc4f7cc9ec5d5e431ec75ace67943f0d48d3abae14f8674c7'
@@ -39,20 +39,41 @@ module Diamant
39
39
 
40
40
  private
41
41
 
42
- def handle_client(client)
43
- begin
44
- r = Net::GeminiRequest.read_new(client)
45
- rescue Net::GeminiBadRequest
46
- client.puts "59\r\n"
47
- return
42
+ def reject_request?(sock, current_load)
43
+ # Accept only 10 thread with no restriction
44
+ return false if current_load < 11
45
+ # Seppuku
46
+ raise 'Server is under heavy load' if current_load > 1965
47
+ if current_load > 42
48
+ @logger.warn '41 - Too much threads...'
49
+ sock.puts "41 See you soon...\r\n"
50
+ return true
48
51
  end
49
- answer = route(r.path)
50
- @logger.info "#{answer[0]} - #{r.uri}"
52
+ # Please wait a little
53
+ @logger.warn '44 5 - Too much threads...'
54
+ sock.puts "44 5\r\n"
55
+ true
56
+ end
57
+
58
+ def handle_client(client)
59
+ current_load = Thread.list.length - 1
60
+ return if reject_request?(client, current_load)
61
+ uri, answer = read_file(client)
62
+ log_line = [current_load, client.peeraddr[3], answer[0]]
63
+ log_line << uri if uri
64
+ @logger.info log_line.join(' - ')
51
65
  answer.each do |line|
52
66
  client.puts "#{line}\r\n"
53
67
  end
54
68
  end
55
69
 
70
+ def read_file(client)
71
+ r = Net::GeminiRequest.read_new(client)
72
+ [r.uri, route(r.path)]
73
+ rescue Net::GeminiBadRequest
74
+ [nil, ["59\r\n"]]
75
+ end
76
+
56
77
  def build_response(route)
57
78
  info = Diamant::MimeType.new(route)
58
79
  answer = IO.readlines route, chomp: true
@@ -8,10 +8,12 @@ module Diamant
8
8
  attr_reader :extension, :content_type
9
9
 
10
10
  MIMETYPES = {
11
+ '.gemini' => 'text/gemini',
11
12
  '.gmi' => 'text/gemini',
12
13
  '.txt' => 'text/plain',
13
14
  '.md' => 'text/markdown',
14
15
  '.org' => 'text/org',
16
+ '.xml' => 'application/xml',
15
17
  '.png' => 'image/png',
16
18
  '.jpg' => 'image/jpeg',
17
19
  '.jpeg' => 'image/jpeg',
@@ -24,7 +26,7 @@ module Diamant
24
26
  end
25
27
 
26
28
  def supported?
27
- @extension == '' || MIMETYPES.has_key?(@extension)
29
+ @extension != '' && MIMETYPES.has_key?(@extension)
28
30
  end
29
31
 
30
32
  private
@@ -32,13 +34,8 @@ module Diamant
32
34
  def extract_info
33
35
  @extension = File.extname @path
34
36
  raise MimeError, "#{@path} format is not supported!" unless supported?
35
- if @extension == ''
36
- # Weird... but we'll try the simple way
37
- @content_type = 'text/plain'
38
- else
39
- # Any other supported extension
40
- @content_type = MIMETYPES[@extension]
41
- end
37
+ # Any other supported extension
38
+ @content_type = MIMETYPES[@extension]
42
39
  end
43
40
  end
44
41
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Diamant
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diamant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Étienne Deparis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-19 00:00:00.000000000 Z
11
+ date: 2020-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-net-text