예제 #1
0
        protected override bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback)
        {
            var urlString = request.GetURL();

            string errorMessage = null;
            int errorStatus = 0;
            string errorStatusText = null;

            try
            {
                var uri = new Uri(urlString);
                var path = uri.Host + uri.AbsolutePath; // ignore host

                var asm = typeof(ClientSchemeHandler).Assembly;
                var resPrefix = "CefGlue.Client.Resources.";

                // convert path to resource name
                var parts = path.Split('/');
                for (var i = 0; i < parts.Length-1; i++)
                {
                    var filename = Path.GetFileNameWithoutExtension(parts[i]);
                    var extension = Path.GetExtension(parts[i]);

                    parts[i] = filename.Replace(".", "._").Replace('-', '_') + extension;
                }

                var resName = resPrefix + string.Join(".", parts);
                this.stream = asm.GetManifestResourceStream(resName);

                if (this.stream != null)
                {
                    // found
                    this.responseLength = -1;
                    this.status = 200;
                    this.statusText = "OK";
                    this.mimeType = GetMimeTypeFromUriSuffix(path);
                    callback.HeadersAvailable();
                    return true;
                }
            }
            catch (Exception ex)
            {
                errorStatus = 500;
                errorStatusText = "Internal Error";
                errorMessage = "<!doctype html><html><body><h1>Internal Error!</h1><pre>" + ex.ToString() + "</pre></body></html>";
            }

            // not found or error while processing request
            errorMessage = errorMessage ?? "<!doctype html><html><body><h1>Not Found!</h1><p>The requested url [" + urlString + "] not found!</p></body></html>";
            var bytes = Encoding.UTF8.GetBytes(errorMessage);
            this.stream = new MemoryStream(bytes, false);

            this.responseLength = -1;
            this.status = errorStatus != 0 ? errorStatus : 404;
            this.statusText = errorStatusText ?? "Not Found";
            this.mimeType = "text/html";
            callback.HeadersAvailable();
            return true;
        }
예제 #2
0
        /// <summary>
        /// Begin processing the request. To handle the request return true and
        /// call HeadersAvailable() once the response header information is
        /// available (HeadersAvailable() can also be called from inside this
        /// method if header information is available immediately). To cancel the request return false.
        /// </summary>
        private int process_request(cef_scheme_handler_t *self, cef_request_t *request, cef_scheme_handler_callback_t *callback)
        {
            ThrowIfObjectDisposed();

            var mRequest  = CefRequest.From(request);
            var mCallback = CefSchemeHandlerCallback.From(callback);

            var handled = this.ProcessRequest(mRequest, mCallback);

            return(handled ? 1 : 0);
        }
예제 #3
0
        /// <summary>
        /// Read response data. If data is available immediately copy up to
        /// |bytes_to_read| bytes into |data_out|, set |bytes_read| to the number
        /// of bytes copied, and return true. To read the data at a later time
        /// set |bytes_read| to 0, return true and call BytesAvailable() when the
        /// data is available. To indicate response completion return false.
        /// </summary>
        private int read_response(cef_scheme_handler_t *self, void *data_out, int bytes_to_read, int *bytes_read, cef_scheme_handler_callback_t *callback)
        {
            ThrowIfObjectDisposed();

            var mCallback = CefSchemeHandlerCallback.From(callback);

            using (var mStream = new UnmanagedMemoryStream((byte *)data_out, bytes_to_read, bytes_to_read, FileAccess.Write))
            {
                int mBytesRead;
                var handled    = this.ReadResponse(mStream, bytes_to_read, out mBytesRead, mCallback);
                *   bytes_read = mBytesRead;
                return(handled ? 1 : 0);
            }
        }
예제 #4
0
 /// <summary>
 /// Begin processing the request.
 ///
 /// To handle the request return true and call HeadersAvailable() once the response header information is
 /// available (HeadersAvailable() can also be called from inside this
 /// method if header information is available immediately).
 ///
 /// To cancel the request return false.
 /// </summary>
 protected abstract bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback);
예제 #5
0
 /// <summary>
 /// Read response data.
 /// If data is available immediately copy up to |bytes_to_read| bytes into |data_out|, set |bytes_read| to the number
 /// of bytes copied, and return true.
 ///
 /// To read the data at a later time set |bytes_read| to 0, return true and call BytesAvailable() when the
 /// data is available.
 ///
 /// To indicate response completion return false.
 /// </summary>
 protected abstract bool ReadResponse(Stream stream, int bytesToRead, out int bytesRead, CefSchemeHandlerCallback callback);
 /// <summary>
 /// Read response data.
 /// If data is available immediately copy up to |bytes_to_read| bytes into |data_out|, set |bytes_read| to the number
 /// of bytes copied, and return true.
 /// 
 /// To read the data at a later time set |bytes_read| to 0, return true and call BytesAvailable() when the
 /// data is available.
 /// 
 /// To indicate response completion return false.
 /// </summary>
 protected abstract bool ReadResponse(Stream stream, int bytesToRead, out int bytesRead, CefSchemeHandlerCallback callback);
 /// <summary>
 /// Begin processing the request.
 /// 
 /// To handle the request return true and call HeadersAvailable() once the response header information is
 /// available (HeadersAvailable() can also be called from inside this
 /// method if header information is available immediately).
 /// 
 /// To cancel the request return false.
 /// </summary>
 protected abstract bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback);
예제 #8
0
 protected override bool ReadResponse(Stream stream, int bytesToRead, out int bytesRead, CefSchemeHandlerCallback callback)
 {
     byte[] buffer = new byte[bytesToRead];
     var readed = this.stream.Read(buffer, 0, buffer.Length);
     if (readed > 0)
     {
         stream.Write(buffer, 0, readed);
         bytesRead = readed;
         return true;
     }
     else
     {
         this.Close();
         bytesRead = 0;
         return false;
     }
 }