-
Notifications
You must be signed in to change notification settings - Fork 0
/
requete.rb
40 lines (33 loc) · 925 Bytes
/
requete.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Requete
require 'socket'
attr_reader :socket , :request , :headers, :body
def initialize(socket)
@socket = socket
self.read
end
def self.read
# request parsing
@request = @socket.gets # On lit sur le socket la 1 ère ligne de la requête
headers = {}
begin
header = @socket.gets
header_name, header_val = header.chomp.split(': ')
@headers[header_name] = header_val
end until header.chomp.empty?
@body = nil
unless headers["Content-Length"].nil? # on teste si le headers a un content-Length
@body = @socket.read(headers["Content-Length"].to_i) # on lit et on stocke le corps de la requête
end
[@request, @headers, @body]
end
def self.cookie?
@headers["Cookie"].nil?
end
def self.cookie
if self.cookie?
champ1,champ2,champ3,champ4 = @headers["Cookie"].chomp.split(';')
champ1.split("=")
else
end
end
end