Class: Knjappserver::Httpsession::Post_multipart

Inherits:
Object
  • Object
show all
Defined in:
lib/include/class_httpsession_post_multipart.rb

Overview

This class parses and handels post-multipart requests.

Defined Under Namespace

Classes: File_upload

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Post_multipart

Returns a new instance of Post_multipart.



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
41
42
# File 'lib/include/class_httpsession_post_multipart.rb', line 5

def initialize(args)
  @args = args
  boundary_regexp = /\A--#{@args["boundary"]}(--)?#{@args["crlf"]}\z/
  @return = {}
  @data = nil
  @mode = nil
  @headers = {}
  @counts = {}
  
  @args["io"].each do |line|
    if boundary_regexp =~ line
      #Finish the data we were writing.
      self.finish_data if @data
      
      @data = ""
      @mode = "headers"
    elsif @mode == "headers"
      if match = line.match(/^(.+?):\s+(.+)#{@args["crlf"]}$/)
        @headers[match[1].to_s.downcase] = match[2]
      elsif line == @args["crlf"]
        @mode = "body"
      else
        raise "Could not match header from: '#{line}'."
      end
    elsif @mode == "body"
      @data << line
    else
      raise "Invalid mode: '#{@mode}'."
    end
  end
  
  self.finish_data if @data and @data.to_s.length > 0
  
  @data = nil
  @headers = nil
  @mode = nil
  @args = nil
end

Instance Attribute Details

#returnObject (readonly)

Returns the value of attribute return.



3
4
5
# File 'lib/include/class_httpsession_post_multipart.rb', line 3

def return
  @return
end

Instance Method Details

#finish_dataObject

Add the current treated data to the return-hash.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/include/class_httpsession_post_multipart.rb', line 45

def finish_data
  @data.chop!
  name = nil
  
  disp = @headers["content-disposition"]
  raise "No 'content-disposition' was given." if !disp
  
  
  #Figure out value-name in post-hash.
  match_name = disp.match(/name=\"(.+?)\"/)
  raise "Could not match name." if !match_name
  name = match_name[1]
  
  
  #Fix count with name if given as increamental [].
  if match = name.match(/^(.+)\[\]$/)
    if !@counts.key?(match[1])
      @counts[match[1]] = 0
    else
      @counts[match[1]] += 1
    end
    
    name = "#{match[1]}[#{@counts[match[1]]}]"
  end
  
  
  #Figure out actual filename.
  match_fname = disp.match(/filename=\"(.+?)\"/)
  
  if match_fname
    obj = Knjappserver::Httpsession::Post_multipart::File_upload.new(
      "fname" => match_fname[1],
      "headers" => @headers,
      "data" => @data
    )
    @return[name] = obj
    @data = nil
    @headers = {}
    @mode = nil
  else
    @return[name] = @data
    @data = nil
    @headers = {}
    @mode = nil
  end
end