Exemplo n.º 1
0
 public void FetchWsdlResponse(OracleInterface ora)
 {
     _headers.Clear();
     _headers.Add(new NameValuePair("Content-Type", "text/xml; charset=utf-8;"));
     _headers.Add(new NameValuePair("Content-Length", ora.WsdlBody().Length.ToString()));
     _responseBody.Append(ora.WsdlBody());
 }
Exemplo n.º 2
0
 public void FetchSoapResponse(OracleInterface ora)
 {
     _headers.Clear();
     _headers.Add(new NameValuePair("Content-Type", "application/soap+xml; charset=utf-8;"));
     _headers.Add(new NameValuePair("Content-Length", ora.SoapBody().Length.ToString()));
     _responseBody.Append(ora.SoapBody());
 }
Exemplo n.º 3
0
        public void FetchXdbResponse(OracleInterface ora)
        {
            ContentType = ora.XdbContentType;
            //ContentType = "application/octet-stream";

            if (ContentType.StartsWith("text"))  //  || contentType.StartsWith("text/html") || contentType.StartsWith("text/xml")
            {
                _responseBody.Append(ora.GetXdbResourceText());
            }
            else
            {
                FileData   = ora.GetXdbResourceFile();
                IsDownload = true;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// The main routine that handles each request, calls the database, and outputs the response from the OWA toolkit back to the client through IIS.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="a"></param>

        private void HandleRequest(object o, EventArgs a)
        {
            // grab the request context from ASP.NET
            HttpApplication app = (HttpApplication)o;
            HttpContext     ctx = (HttpContext)app.Context;

            if (DadConfiguration.HideServerBanner)
            {
                // http://blogs.technet.com/b/stefan_gossner/archive/2008/03/12/iis-7-how-to-send-a-custom-server-http-header.aspx

                try
                {
                    app.Response.Headers.Remove("Server");
                    app.Response.Headers.Remove("X-AspNet-Version");
                    app.Response.Headers.Remove("X-Powered-By");
                }
                catch (PlatformNotSupportedException e)
                {
                    logger.Warn("Attempted to hide server banners (HideServerBanner = True), but failed with error: " + e.Message);
                }
            }

            // check if gateway should be bypassed (return normal execution to IIS)
            if (DadConfiguration.ServeStaticContent)
            {
                string physicalPath = app.Request.PhysicalPath;

                if (File.Exists(physicalPath))
                {
                    logger.Debug("Requested file " + physicalPath + " exists on disk. Gateway will not handle this request.");
                    return;
                }
                else
                {
                    logger.Debug("Requested file " + physicalPath + " was NOT found on disk, continuing with normal gateway request.");
                }
            }

            // parse the request (URL); we are expecting calls in a format similar to the following:
            // http://servername/PLSQLGatewayModule/dadname/[schema.][package.]procedure?parameter1=xxx&parameter2=yyy

            string serverName         = app.Request.ServerVariables["HTTP_HOST"];
            string requestContentType = app.Request.ContentType;
            string requestBody        = "";
            string requestPath        = app.Request.FilePath.Substring(1);
            string soapAction         = app.Request.Headers["SOAPAction"];

            if (requestContentType.ToLower() != "application/x-www-form-urlencoded")
            {
                requestBody = new StreamReader(app.Request.InputStream, System.Text.Encoding.Default).ReadToEnd();
                requestBody = HttpUtility.UrlDecode(requestBody);
            }

            GatewayRequest gReq = new GatewayRequest(serverName, app.Request.HttpMethod, app.Request.FilePath, app.Request.RawUrl, soapAction);

            if (!gReq.DadSpecifiedInRequest)
            {
                if (DadConfiguration.DefaultDadEnabled)
                {
                    string defaultURL = "/" + gReq.ModuleName + "/" + gReq.DadName + "/" + gReq.ProcName;

                    logger.Debug("DAD not specified in URL, or specified DAD not defined in configuration file. Redirecting to default DAD and default procedure: " + defaultURL);
                    ctx.Response.Redirect(defaultURL);
                    //ctx.Response.StatusCode = 302; // moved temporarily
                    app.CompleteRequest();
                    return;
                }
                else
                {
                    logger.Warn("DAD not specified in request, or DAD not defined in configuration file, and default DAD disabled. Request terminated with 404.");
                    throw new HttpException(404, "Not Found");
                }
            }

            if (gReq.ValidRequest)
            {
                // the requested procedure name is valid
                // now process querystring and form variables, set up the OWA packages with the CGI environment information,
                // and check if there are files to be uploaded

                if (gReq.IsWsdlRequest)
                {
                    logger.Debug("Request is for WSDL document");
                }
                else if (gReq.IsSoapRequest)
                {
                    logger.Debug("Invocation protocol is SOAP");
                    gReq.AddRequestParametersForSOAP(gReq.ProcName, requestBody);
                }
                else
                {
                    gReq.AddRequestParameters(app.Request.QueryString, app.Request.Form, app.Request.Files, requestBody);
                }

                gReq.AddCGIEnvironment(ctx.Request.ServerVariables);

                OracleParameterCache opc = new OracleParameterCache(ctx);

                // connect to the database
                OracleInterface ora = new OracleInterface(gReq, opc);

                if (ora.Connected())
                {
                    ora.SetupOwaCGI(gReq.CGIParameters, ctx.Request.UserHostName, ctx.Request.UserHostAddress, gReq.BasicAuthUsername, gReq.BasicAuthPassword);

                    if (ctx.Request.Files.Count > 0)
                    {
                        bool uploadSuccess = ora.UploadFiles(gReq.UploadedFiles);
                    }
                }

                bool success = false;

                // the GatewayResponse object will hold the result of the database call (headers, cookies, status codes, response body, etc.)
                GatewayResponse gResp = new GatewayResponse();

                if (!ora.Connected())
                {
                    logger.Debug("Failed to connect to database, skipping procedure execution.");
                    success = false;
                }
                else if (gReq.IsSoapRequest)
                {
                    if (gReq.IsWsdlRequest)
                    {
                        success = ora.GenerateWsdl(gReq.ServerName, gReq.ModuleName, gReq.DadName, gReq.ProcName);
                    }
                    else
                    {
                        success = ora.ExecuteMainProc(gReq.OwaProc, gReq.RequestParameters, false, gReq.ProcName);
                        if (success)
                        {
                            ora.GenerateSoapResponse(gReq.ProcName);
                        }
                        else
                        {
                            int    errorCode = ora.GetLastErrorCode();
                            string errorText = ora.GetLastErrorText();
                            logger.Debug("SOAP request failed with error code " + errorCode + ", generating SOAP Fault response.");
                            ora.GenerateSoapFault(errorCode, errorText);
                        }
                    }
                }
                else if (gReq.IsFlexibleParams)
                {
                    logger.Debug("Using flexible parameter mode (converting parameters to name/value arrays)");
                    success = ora.ExecuteMainProc(gReq.OwaProc, gReq.GetFlexibleParams(), false, gReq.ProcName);
                }
                else if (gReq.IsPathAlias)
                {
                    logger.Debug("Forwarding request to PathAliasProcedure");
                    success = ora.ExecuteMainProc(gReq.OwaProc, gReq.GetPathAliasParams(), false, gReq.ProcName);
                }
                else if (gReq.IsXdbAlias)
                {
                    logger.Debug("Forwarding request to XDB Repository");
                    success = ora.GetXdbResource(gReq.XdbAliasValue);
                }
                else if (gReq.IsDocumentPath)
                {
                    logger.Debug("Forwarding request to DocumentProcedure");
                    success = ora.ExecuteMainProc(gReq.OwaProc, new List <NameValuePair>(), false, gReq.ProcName);
                }
                else
                {
                    success = ora.ExecuteMainProc(gReq.OwaProc, gReq.RequestParameters, false, gReq.ProcName);

                    if (!success && ora.GetLastErrorText().IndexOf("PLS-00306:") > -1)
                    {
                        logger.Error("Call failed: " + ora.GetLastErrorText());
                        logger.Debug("Wrong number or types of arguments in call. Will retry call after looking up parameter metadata in data dictionary.");
                        success = ora.ExecuteMainProc(gReq.OwaProc, gReq.RequestParameters, true, gReq.ProcName);
                    }
                    else if (!success && ora.GetLastErrorText().IndexOf("ORA-01460:") > -1)
                    {
                        logger.Error("Call failed: " + ora.GetLastErrorText());
                        logger.Debug("Unimplemented or unreasonable conversion requested. Will retry call after looking up parameter metadata in data dictionary.");
                        success = ora.ExecuteMainProc(gReq.OwaProc, gReq.RequestParameters, true, gReq.ProcName);
                    }
                    else if (!success && ora.GetLastErrorText().IndexOf("ORA-00201:") > -1)
                    {
                        logger.Error("Call failed: " + ora.GetLastErrorText());
                        logger.Debug("Identifier must be declared. Will retry call after looking up parameter metadata in data dictionary.");
                        success = ora.ExecuteMainProc(gReq.OwaProc, gReq.RequestParameters, true, gReq.ProcName);
                    }
                }

                if (success)
                {
                    logger.Info("Gateway procedure executed successfully.");
                    ora.DoCommit();

                    if (gReq.IsWsdlRequest)
                    {
                        logger.Info("Responding with WSDL document");
                        gResp.FetchWsdlResponse(ora);
                    }
                    else if (gReq.IsSoapRequest)
                    {
                        logger.Info("Responding with SOAP response");
                        gResp.FetchSoapResponse(ora);
                    }
                    else if (gReq.IsXdbAlias)
                    {
                        logger.Info("Responding with XDB Resource");
                        gResp.FetchXdbResponse(ora);
                    }
                    else
                    {
                        // fetch the response buffers from OWA
                        logger.Debug("Fetch buffer size = " + gReq.DadConfig.FetchBufferSize);
                        gResp.FetchOwaResponse(ora);
                    }

                    ora.CloseConnection();

                    // process response
                    ProcessResponse(gReq, gResp, ctx, app);
                }
                else
                {
                    logger.Error("Call failed: " + ora.GetLastErrorText());

                    ora.DoRollback();

                    if (gReq.IsSoapRequest)
                    {
                        logger.Debug("SOAP request failed, returning SOAP fault as part of normal response.");
                        gResp.FetchSoapResponse(ora);
                        ProcessResponse(gReq, gResp, ctx, app);
                    }
                    else if (gReq.DadConfig.ErrorStyle == "DebugStyle")
                    {
                        logger.Error("Request failed, showing debug error page");
                        ctx.Response.Write(GatewayError.GetErrorDebugPage(gReq, ora.GetLastErrorText()));
                    }
                    else
                    {
                        logger.Error("Request failed, user gets status code 404");
                        throw new HttpException(404, "Not Found");
                        //ctx.Response.Clear();
                        //ctx.Response.StatusCode = 404;
                    }

                    // TODO: does this get called if HttpException is thrown above... don't think so!
                    ora.CloseConnection();
                }
            }
            else
            {
                logger.Warn("Request (" + requestPath + ") not valid, returning 404...");

                if (gReq.DadConfig.ErrorStyle == "DebugStyle")
                {
                    ctx.Response.Write(GatewayError.GetInvalidRequestDebugPage(requestPath, gReq.DadConfig));
                }
                else
                {
                    throw new HttpException(404, "Not Found");
                    //ctx.Response.Clear();
                    //ctx.Response.StatusCode = 404;
                }
            }

            app.CompleteRequest();
            logger.Info("Request completed.");
        }
Exemplo n.º 5
0
        public void FetchOwaResponse(OracleInterface ora)
        {
            int pageFragmentCount = 0;

            while (ora.MoreToFetch())
            {
                pageFragmentCount = pageFragmentCount + 1;

                if (pageFragmentCount == 1)
                {
                    string firstFragment = ora.GetOwaPageFragment();

                    ParseResponseHeaders(firstFragment);

                    // check if we are downloading a file, if so ignore any OWA/HTP generated output (except the headers)
                    //IsDownload = ora.IsDownload();
                    IsDownload = ora.IsDownload;

                    if (IsDownload)
                    {
                        // get the file info
                        string downloadInfo = ora.GetDownloadInfo();

                        if (downloadInfo.Equals("B") || downloadInfo.Equals("F"))
                        {
                            // get the file
                            FileData = ora.GetDownloadFile(downloadInfo);
                        }
                        else
                        {
                            // decode the file info (filename, last_updated, mime_type, content_type, dad_charset, doc_size)
                            if (downloadInfo.StartsWith("12XNOT_MODIFIED"))
                            {
                                StatusCode = 302;
                                //StatusDescription = "Not Modified";
                            }
                            else
                            {
                                int    fileNameStartPos = downloadInfo.IndexOf("X") + 1;
                                int    fileNameLength   = int.Parse(downloadInfo.Substring(0, fileNameStartPos - 1));
                                string fileName         = downloadInfo.Substring(fileNameStartPos, fileNameLength);

                                string leftToParse = downloadInfo.Substring(fileNameStartPos + fileNameLength + 1);

                                int    lastModifiedStartPos = leftToParse.IndexOf("X") + 1;
                                int    lastModifiedLength   = int.Parse(leftToParse.Substring(0, lastModifiedStartPos - 1));
                                string lastModified         = leftToParse.Substring(lastModifiedStartPos, lastModifiedLength);

                                leftToParse = leftToParse.Substring(lastModifiedStartPos + lastModifiedLength + 1);

                                int    contentTypeStartPos = leftToParse.IndexOf("X") + 1;
                                int    contentTypeLength   = int.Parse(leftToParse.Substring(0, contentTypeStartPos - 1));
                                string contentType         = leftToParse.Substring(contentTypeStartPos, contentTypeLength);

                                leftToParse = leftToParse.Substring(contentTypeStartPos + contentTypeLength + 1);

                                // skip other encoded fields, chop off last X, content-length is last
                                leftToParse = leftToParse.Substring(0, leftToParse.Length - 1);

                                int contentLengthStartPos = leftToParse.LastIndexOf("X");
                                int contentLength         = int.Parse(leftToParse.Substring(contentLengthStartPos + 1));

                                FileData      = ora.GetDownloadFileFromDocTable(fileName);
                                ContentType   = contentType;
                                ContentLength = contentLength;
                                _headers.Add(new NameValuePair("Last-Modified", lastModified));
                            }
                        }

                        break; // exit loop since we do not need to fetch more from the HTP buffer
                    }
                }
                else
                {
                    // second or later fragment, just get the text
                    _responseBody.Append(ora.GetOwaPageFragment());
                }
            }
        }