Module: ClientPkgServe

Defined in:
plugins/client_pkg/lib/client_pkg_serve.rb

Instance Method Summary collapse

Instance Method Details

#broker_urlsObject



11
12
13
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 11

def broker_urls
  ::RSence.config[:broker_urls]
end

#build_busy_waitObject



24
25
26
27
28
29
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 24

def build_busy_wait
  while @build_busy
    puts "-- build not finished, waiting.. --"
    sleep 0.4
  end
end

#check_ua(request) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 43

def check_ua( request )
  support_gzip = (request.header.has_key?('Accept-Encoding') and request.header['Accept-Encoding'].include?('gzip'))
  support_gzip = false if ::RSence.config[:no_gzip]
  if request.header.has_key?('User-Agent')
    ua = request.header['User-Agent']
    is_symbian = ua.include?("SymbianOS")
    is_safari  = ((not is_symbian) and ua.include?("WebKit"))
    is_msie = ((not ua.include?("Opera")) and ua.include?("MSIE"))
    is_msie6 = ((not ua.include?("Opera")) and ua.include?("MSIE 6.0"))
  end
  if is_safari
    version = ua.split( 'WebKit/' )[1].split('.')[0].to_i
  else
    version = 0 # not used for others
  end
  return {
    :symbian => is_symbian,
    :safari  => is_safari,
    :msie    => is_msie,
    :msie6   => is_msie6,
    :version => version
  }
end

#get(request, response, session) ⇒ Object

Responds to get-requests



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 225

def get( request, response, session )
  
  build_busy_wait
  
  ua = check_ua( request )
  
  set_headers( response )
  
  ## Split path into an array for determining what to serve
  request_uri = '/'+request.path.match( /^#{::RSence.config[:broker_urls][:h]}(.*)$/ )[1]
  
  request_path = request_uri.split( '/' )
  
  ## Requested type of client resource (js/themes)
  req_type = request_path[2]
  
  unless ['js','themes'].include? req_type
    req_rev = req_type
    req_type = request_path[3]
    request_path.delete_at(2)
  end
  
  ## Special rules for the special browser
  if request.path.end_with?('.htc')
    serve_htc( request, response, request_path )
  ## Serve compiled client javascript component files:
  elsif req_type == 'js'
    serve_js( request, response, request_path, ua )
  ## Serve client theme files
  elsif req_type == 'themes'
    serve_theme( request, response, request_path, ua )
  end
  
end

#httime(time) ⇒ Object

Helper method to return the time formatted according to the HTTP RFC



20
21
22
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 20

def httime(time)
  return time.gmtime.strftime('%a, %d %b %Y %H:%M:%S %Z')
end

#match(uri, request_type) ⇒ Object



15
16
17
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 15

def match( uri, request_type )
  uri.match( /^#{broker_urls[:h]}/ )
end

#serve_htc(request, response, request_path) ⇒ Object



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
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 72

def serve_htc( request, response, request_path )
  req_file = request_path[3]
  
  # this file is a part of iefix, it injects calls to
  # the ie rendering engine to override stupid behavior
  # when changing element properties
  if request_path.include?('ie_css_element.htc')
    response.status = 200
    response['Content-Type'] = 'text/x-component'
    ## Usually, we don't need it, because the client framework does calls when needed
    response.body = %{<PUBLIC:COMPONENT lightWeight="true"></PUBLIC:COMPONENT>}
    
    ## Enable it to call iefix automatically whenever element properties are changed
    #response.body = %{<PUBLIC:COMPONENT lightWeight="true">\r\n<script type="text/javascript">\r\ntry{element.attachEvent("onpropertychange",iefix.htcElementEntry);}catch(e){}\r\n</script>\r\n</PUBLIC:COMPONENT>}
  
  # this file is a part of iefix, it injects calls to
  # the ie rendering engine to override stupid behavior
  # when changing style properties
  elsif request_path.include?('ie_css_style.htc')
    response.status = 200
    response['Content-Type'] = 'text/x-component'
    
    ## Usually, we don't need it, because the client framework does calls when needed
    response.body = %{<PUBLIC:COMPONENT lightWeight="true"></PUBLIC:COMPONENT>}
    
    ## Enable it to call iefix automatically whenever element properties are changed
    #response.body = %{<PUBLIC:COMPONENT lightWeight="true">\r\n<script type="text/javascript">\r\ntry{element.attachEvent("onreadystatechange",iefix.htcStyleEntry);}catch(e){}\r\n</script>\r\n</PUBLIC:COMPONENT>}
  
  # Other htc requests are invalid
  else
    response.status = 503
    response.body   = '503 - Invalid Request'
  end
end

#serve_js(request, response, request_path, ua) ⇒ Object



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
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 107

def serve_js( request, response, request_path, ua )
  # the file-specific identifier ('core', 'basic' etc)
  req_file = request_path[3][0..-4]
  
  if not @client_cache.js_cache.has_key?( req_file )
    response.status = 404
    response.body   = '/* 404 - Not Found */'
  else
    
    response.status = 200
    response['Content-Type'] = 'text/javascript; charset=utf-8'
    
    # these browsers have issues with gzipped js content
    support_gzip = support_gzip?( ua )
    
    if support_gzip
      #response['Transfer-Encoding'] = 'chunked,gzip'
      response['Last-Modified'] = @client_cache.last_modified
      body = @client_cache.gz_cache[ req_file ]+"\r\n\r\n"
      response['Content-Length'] = body.bytesize.to_s
      response['Content-Encoding'] = 'gzip'
      response.body   = body
    else
      
      response['Last-Modified'] = @client_cache.last_modified
      body = @client_cache.js_cache[ req_file ]
      response['Content-Length'] = body.bytesize.to_s
      response.body = body
      
    end
  end
end

#serve_theme(request, response, request_path, ua) ⇒ Object



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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 140

def serve_theme( request, response, request_path, ua )
  # Get the name of the theme
  theme_name = request_path[3]
  
  # Get the theme resource type (html/css/gfx)
  theme_part = request_path[4].to_sym
  
  # Get the theme resource identifier
  req_file  = request_path[5]
  
  # checks for theme name
  has_theme = @client_cache.theme_cache.has_key?( theme_name )
  
  # checks for theme part (css/html/gfx)
  has_theme_part = ( has_theme and @client_cache.theme_cache[theme_name].has_key?( theme_part ) )
  
  # checks for theme file
  has_theme_file = ( has_theme_part and @client_cache.theme_cache[theme_name][theme_part].has_key?( req_file ) )
  
  if not has_theme_file and req_file == 'common.css'
    response.status = 200
    response['Content-Type'] = 'text/css'
    response.body = ''
  end
  
  if not has_theme
    response.status = 404
    response.body   = '404 - Theme Not Found'
    puts "Theme #{theme_name} not found, avail: #{@client_cache.theme_cache.keys.join(', ')}" if RSence.args[:verbose]
  elsif not has_theme_part
    response.status = 503
    response.body   = '503 - Invalid Theme Part Request'
  elsif not has_theme_file
    response.status = 404
    response.body   = '404 - Theme Resource Not Found'
    puts "File not found, avail: #{@client_cache.theme_cache[theme_name][theme_part].keys.join(', ')}" if RSence.args[:verbose]
  else
    
    response.status = 200
    
    file_ext = req_file.split('.')[-1]
    response['Content-Type'] = {
      'html' => 'text/html; charset=utf-8',
      'css'  => 'text/css; charset=utf-8',
      'png'  => 'image/png',
      'jpg'  => 'image/jpeg',
      'gif'  => 'image/gif',
      'swf'  => 'application/x-shockwave-flash'
    }[file_ext]
    
    if theme_part == :gfx
      support_gzip = false
    else
      support_gzip = support_gzip?( ua )
    end
    
    if support_gzip
      response['Last-Modified'] = @client_cache.last_modified
      body = @client_cache.theme_cache[theme_name][theme_part][ req_file+'.gz' ]
      response['Content-Length'] = body.bytesize.to_s
      response['Content-Encoding'] = 'gzip'
      response.body   = body
    else
      
      # Special IE6 condition to serve gifs instead of png's, because it works much better
      # than using the ActiveX alpha filter hack
      if ua[:msie6] and req_file[-4..-1] == '.png'
        ie6_req_png2gif = req_file.gsub('.png','-ie6.gif')
        req_file = ie6_req_png2gif if @client_cache.theme_cache[theme_name][theme_part].include?(ie6_req_png2gif)
      end
    
      response['Last-Modified'] = @client_cache.last_modified
      body = @client_cache.theme_cache[theme_name][theme_part][ req_file ]
      if body.nil?
        warn "ClientPkgServe#get: empty body for #{request.path}"
        body = ''
      end
      response['Content-Length'] = body.bytesize.to_s
      response.body = body
      
    end
  end
end

#set_headers(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 31

def set_headers( response )
  # Sets the response date header to the current time:
  response['Date'] = httime( Time.now )
  
  # Controls caching with headers based on the configuration
  if ::RSence.config[:cache_maximize]
    response['Expires'] = httime(Time.now+::RSence.config[:cache_expire])
  else
    response['Cache-Control'] = 'no-cache'
  end
end

#support_gzip?(ua) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'plugins/client_pkg/lib/client_pkg_serve.rb', line 67

def support_gzip?( ua )
  doesnt_support = ( ua[:msie6] or ua[:symbian] or (ua[:safari] and ua[:version] < 533) )
  return ( not doesnt_support )
end