File: common.rb

package info (click to toggle)
libnora-ruby 1%3A0.0.20041021-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 408 kB
  • ctags: 726
  • sloc: ruby: 5,186; ansic: 669; makefile: 248; sql: 10
file content (196 lines) | stat: -rwxr-xr-x 3,817 bytes parent folder | download | duplicates (5)
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
41
42
43
44
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Web::Common
# Copyright(c) 2002 MoonWolf <moonwolf@moonwolf.com>
require "socket"
require "thread"
require 'digest/md5'
require 'web/escape'

module Web
  module Common
    
    MUTEX = Mutex.new
    @@counter = rand(65536)
    HOST = Socket.gethostbyname(Socket.gethostname)[3]
    
    def self.unique_id()
      tm = Time.now.gmtime
      key = [tm.tv_sec, tm.tv_usec % 65536, Process.pid, Thread.current.__id__ ].pack("NnNN")
      key << HOST
      MUTEX.synchronize {
        @@counter = (@@counter + 1) % 65536
        key << [@@counter].pack("n")
      }
      
      [key].pack("m").chop.tr("+/","@-")
    end
    
    # Parse Semi-colon delimited values
    #  key1=value ; key2="value" ; key3
    #    => {"key1"="value", "key2"=>"value", "key3"=>nil}
    def self.parse_semi(str)
      hash = Hash.new
      str.scan(/(\w+)(?:\s*=\s*(?:(\w+)|"(.*?)"))?/) {
        hash[$1] = $2 || $3
      }
      hash
    end

    # Time to RFC1123 String
    def self.rfc1123date(tm)
      tm = tm.clone.gmtime
      tm.strftime("%a, %d %b %Y %H:%M:%S GMT")
    end

    #
    class ParamHash
      include Enumerable
      #
      def initialize
        @h = Hash.new
      end

      def to_hash
        hash = {}
        @h.each {|key,values|
          hash[key] = values.first
        }
        hash
      end

      private
      #L[𐳋K
      def normalize(key)
        key
      end

      public
      # for Enumberable
      def each
        return self unless block_given?
        @h.each {|k,v|
          yield k,v
        }
        self
      end

      # L[lPŽo
      # l݂ȂnilԂ
      def get(key,index=0)
        key = normalize(key)
        return nil unless @h.has_key?(key)
        return @h[key][index]
      end
      
      # L[ɒlݒ肷
      def set(key, value, index=0)
        key = normalize(key)
        arr = @h.fetch(key, [])
        arr[index] = value
        @h[key] = arr
      end
      
      def [](key, index=0)
        key = normalize(key)
        if index
          get(key, index)
        else
          @h.fetch(key,[])
        end
      end
      
      def []=(key, *arg)
        key = normalize(key)
        if arg.size>1
          index = arg.shift
          if index
            set(key, arg.first, index)
          else
            @h[key] = @h.fetch(key,[]) << arg
          end
        else
          set(key, arg.first, 0)
        end
      end

      def add(key,value)
        key = normalize(key)
        arr = @h.fetch(key, [])
        arr << value
        @h[key] = arr
      end

      def clear
        @h.clear
      end

      def delete(key)
        key = normalize(key)
        @h.delete(key)
      end

      def has_key?(key)
        key = normalize(key)
        @h.has_key?(key)
      end

      def include?(key)
        has_key?(key)
      end

      def key?(key)
        has_key?(key)
      end

      def member?(key)
        has_key?(key)
      end

      def keys
        @h.keys
      end

      def size
        @h.size
      end

      def length
        @h.length
      end

    end # ParamHash

    # HTTPwb_[ێ
    class Header < ParamHash
      private
      def normalize(key)
        key.downcase
      end
    end


    #Abv[hꂽt@C
    class FileData
      def initialize(filename, content_type)
        @filename     = filename
        @content_type = content_type
        basename = 'web_' + Web::Common::unique_id
        @path = File.join('/tmp',basename)
      end
      attr_accessor :filename, :content_type, :path
      
      def cleanup
        File.unlink @path
      end
      
      def to_s
        str = nil
        open(@path,'rb') {|f|
          str = f.read || ''
        }
        str
      end
      
    end

  end # Common
end # Web