Package backend :: Package server :: Module apacheUploadServer
[hide private]
[frames] | no frames]

Source Code for Module backend.server.apacheUploadServer

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16   
 17  import string 
 18  import sys 
 19  from spacewalk.common import apache 
 20   
 21  import rhnSession 
 22   
 23  from spacewalk.common import rhnFlags 
 24  from spacewalk.common.rhnLog import log_debug, log_error, log_setreq, initLOG 
 25  from spacewalk.common.rhnConfig import CFG, initCFG 
 26  from spacewalk.common.rhnException import rhnFault 
 27  from spacewalk.common.rhnTB import Traceback 
 28  from spacewalk.server import rhnImport 
 29   
 30   
31 -class UploadHandler:
32
33 - def __init__(self):
34 self.servers = {} 35 self.server = None
36
37 - def headerParserHandler(self, req):
38 log_setreq(req) 39 # init configuration options with proper component 40 options = req.get_options() 41 # if we are initializing out of a <Location> handler don't 42 # freak out 43 if "RHNComponentType" not in options: 44 # clearly nothing to do 45 return apache.OK 46 initCFG(options["RHNComponentType"]) 47 initLOG(CFG.LOG_FILE, CFG.DEBUG) 48 if req.method == 'GET': 49 # This is the ping method 50 return apache.OK 51 self.servers = rhnImport.load("upload_server/handlers", 52 interface_signature='upload_class') 53 if 'SERVER' not in options: 54 log_error("SERVER not set in the apache config files!") 55 return apache.HTTP_INTERNAL_SERVER_ERROR 56 server_name = options['SERVER'] 57 if server_name not in self.servers: 58 log_error("Unable to load server %s from available servers %s" % 59 (server_name, self.servers)) 60 return apache.HTTP_INTERNAL_SERVER_ERROR 61 server_class = self.servers[server_name] 62 self.server = server_class(req) 63 return self._wrapper(req, "headerParserHandler")
64
65 - def handler(self, req):
66 if req.method == 'GET': 67 # This is the ping method 68 log_debug(1, "GET method received, returning") 69 req.headers_out['Content-Length'] = '0' 70 # pkilambi:check for new version of rhnpush to differentiate 71 # new sats from old satellites. 72 req.headers_out['X-RHN-Check-Package-Exists'] = '1' 73 req.send_http_header() 74 return apache.OK 75 return self._wrapper(req, "handler")
76
77 - def cleanupHandler(self, req):
78 if req.method == 'GET': 79 # This is the ping method 80 return apache.OK 81 retval = self._wrapper(req, "cleanupHandler") 82 # Reset the logger to stderr 83 initLOG() 84 self.server = None 85 return retval
86
87 - def logHandler(self, req):
88 if req.method == 'GET': 89 # This is the ping method 90 return apache.OK 91 retval = self._wrapper(req, "logHandler") 92 return retval
93
94 - def _wrapper(self, req, function_name):
95 #log_debug(1, "_wrapper", req, function_name) 96 if not hasattr(self.server, function_name): 97 log_error("%s doesn't have a %s function" % 98 (self.server, function_name)) 99 return apache.HTTP_NOT_FOUND 100 function = getattr(self.server, function_name) 101 try: 102 log_debug(5, "Calling", function) 103 ret = function(req) 104 except rhnFault: 105 e = sys.exc_info()[1] 106 log_debug(4, "rhnFault caught: %s" % (e, )) 107 error_string = self._exception_to_text(e) 108 error_code = e.code 109 self._error_to_headers(req.err_headers_out, error_code, error_string) 110 ret = rhnFlags.get("apache-return-code") 111 if not ret: 112 ret = apache.HTTP_INTERNAL_SERVER_ERROR 113 req.status = ret 114 log_debug(4, "_wrapper %s exited with apache code %s" % 115 (function_name, ret)) 116 except rhnSession.ExpiredSessionError: 117 e = sys.exc_info()[1] 118 # if session expires we catch here and return a forbidden 119 # abd make it re-authenticate 120 log_debug(4, "Expire Session Error Caught: %s" % (e, )) 121 return 403 122 except: 123 Traceback("server.apacheUploadServer._wrapper", req=req) 124 log_error("Unhandled exception") 125 return apache.HTTP_INTERNAL_SERVER_ERROR 126 return ret
127 128 # Adds an error code and error string to the headers passed in
129 - def _error_to_headers(self, headers, error_code, error_string):
130 error_string = string.strip(error_string) 131 import base64 132 error_string = string.strip(base64.encodestring(error_string)) 133 for line in map(string.strip, string.split(error_string, '\n')): 134 headers.add(self.server.error_header_prefix + '-String', line) 135 headers[self.server.error_header_prefix + '-Code'] = str(error_code)
136
137 - def _exception_to_text(self, exception):
138 return """\ 139 Error Message: 140 %s 141 Error Class Code: %s 142 Error Class Info: %s 143 """ % (string.strip(exception.text), exception.code, 144 string.rstrip(exception.arrayText))
145 146 # Instantiate external entry points: 147 apache_server = UploadHandler() 148 149 HeaderParserHandler = apache_server.headerParserHandler 150 Handler = apache_server.handler 151 CleanupHandler = apache_server.cleanupHandler 152 LogHandler = apache_server.logHandler 153