public Stream GetInputStream()
        {
            try
            {
                // CSDMain #133228: "Consume GetBufferlessInputStream"
                // The ReadEntityBodyMode property on the HttpRequest keeps track of whether the request stream has already been accessed, and if so, what API was used to access the request.
                //     - "None" means that the request stream hasn't been accessed.
                //     - "Bufferless" means that GetBufferlessInputStream() was used to access it.
                //     - "Buffered" means GetBufferedInputStream() was used to access it.
                //     - "Classic" means that either the InputStream, Form, Files, or BinaryRead APIs were invoked already.
                // In general, these values are incompatible with one another, meaning that once the request was accessed in a "Classic" way, only "Classic" APIs can be invoked on the HttpRequest.
                // If incompatible APIs are invoked, an HttpException is thrown.
                // In order to prevent HttpExceptions from being thrown for this reason, we will check the ReadEntityBodyMode, and access the request stream with the corresponding API
                // If the request stream hasn't been accessed yet (eg, by an HttpModule which executed earlier), then we will use GetBufferlessInputStream by default.
                ReadEntityBodyMode mode = this.context.Request.ReadEntityBodyMode;
                Fx.Assert(mode == ReadEntityBodyMode.None || mode == ReadEntityBodyMode.Bufferless || mode == ReadEntityBodyMode.Buffered || mode == ReadEntityBodyMode.Classic,
                          "Unknown value for System.Web.ReadEntityBodyMode enum");

                if (mode == ReadEntityBodyMode.None && ServiceHostingEnvironment.AspNetCompatibilityEnabled && AppSettings.UseClassicReadEntityMode)
                {
                    mode = ReadEntityBodyMode.Classic;
                }

                switch (mode)
                {
                case ReadEntityBodyMode.None:
                case ReadEntityBodyMode.Bufferless:
                    return(this.context.Request.GetBufferlessInputStream(true));     // ignores system.web/httpRuntime/maxRequestLength

                case ReadEntityBodyMode.Buffered:
                    return(this.context.Request.GetBufferedInputStream());

                default:
                    // ReadEntityBodyMode.Classic:
                    return(this.context.Request.InputStream);
                }
            }
            catch (HttpException hostedException)
            {
                if (hostedException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
                {
                    throw FxTrace.Exception.AsError(HttpInput.CreateHttpProtocolException(SR.Hosting_MaxRequestLengthExceeded, HttpStatusCode.RequestEntityTooLarge, null, hostedException));
                }
                else
                {
                    throw FxTrace.Exception.AsError(new CommunicationException(hostedException.Message, hostedException));
                }
            }
        }
Beispiel #2
0
        /*
         * Read entire raw content as byte array
         */
        private HttpRawUploadedContent GetEntireRawContent() {
            if (_wr == null)
                return null;

            if (_rawContent != null) {
                // if _rawContent was set by HttpBufferlessInputStream, then we will apply the filter here
                if (_installedFilter != null && !_filterApplied) {
                    ApplyFilter(ref _rawContent, RuntimeConfig.GetConfig(_context).HttpRuntime.RequestLengthDiskThresholdBytes);
                }
                return _rawContent;
            }

            if (_readEntityBodyMode == ReadEntityBodyMode.None) {
                _readEntityBodyMode = ReadEntityBodyMode.Classic;
            }
            else if (_readEntityBodyMode == ReadEntityBodyMode.Buffered) {
                // _rawContent should have been set already
                throw new InvalidOperationException(SR.GetString(SR.Invalid_operation_with_get_buffered_input_stream));
            }
            else if (_readEntityBodyMode == ReadEntityBodyMode.Bufferless) {
                throw new HttpException(SR.GetString(SR.Incompatible_with_get_bufferless_input_stream));
            }

            // enforce the limit
            HttpRuntimeSection cfg = RuntimeConfig.GetConfig(_context).HttpRuntime;
            int limit = cfg.MaxRequestLengthBytes;
            if (ContentLength > limit) {
                if ( !(_wr is IIS7WorkerRequest) ) {
                    Response.CloseConnectionAfterError();
                }
                throw new HttpException(SR.GetString(SR.Max_request_length_exceeded),
                                        null, WebEventCodes.RuntimeErrorPostTooLarge);
            }

            // threshold to go to file

            int fileThreshold = cfg.RequestLengthDiskThresholdBytes;

            // read the preloaded content

            HttpRawUploadedContent rawContent = new HttpRawUploadedContent(fileThreshold, ContentLength);

            byte[] preloadedContent = _wr.GetPreloadedEntityBody();

            if (preloadedContent != null) {
                _wr.UpdateRequestCounters(preloadedContent.Length);
                rawContent.AddBytes(preloadedContent, 0, preloadedContent.Length);
            }

            // read the remaing content

            if (!_wr.IsEntireEntityBodyIsPreloaded()) {
                int remainingBytes = (ContentLength > 0) ? ContentLength - rawContent.Length : Int32.MaxValue;

                HttpApplication app = _context.ApplicationInstance;
                byte[] buf = (app != null) ? app.EntityBuffer : new byte[8 * 1024];
                int numBytesRead = rawContent.Length;

                while (remainingBytes > 0) {
                    int bytesToRead = buf.Length;
                    if (bytesToRead > remainingBytes)
                        bytesToRead = remainingBytes;

                    int bytesRead = _wr.ReadEntityBody(buf, bytesToRead);
                    if (bytesRead <= 0)
                        break;

                    _wr.UpdateRequestCounters(bytesRead);

                    rawContent.AddBytes(buf, 0, bytesRead);

                    remainingBytes -= bytesRead;
                    numBytesRead += bytesRead;

                    if (numBytesRead > limit) {
                        throw new HttpException(SR.GetString(SR.Max_request_length_exceeded),
                                    null, WebEventCodes.RuntimeErrorPostTooLarge);
                    }

                    // Fail synchrously if receiving the request content takes too long
                    // RequestTimeoutManager is not efficient in case of ThreadPool starvation
                    // as the timer callback doing Thread.Abort may not trigger for a long time
                    if (remainingBytes > 0 && _context.HasTimeoutExpired) {
                        throw new HttpException(SR.GetString(SR.Request_timed_out));
                    }
                }
            }

            rawContent.DoneAddingBytes();

            // filter content
            if (_installedFilter != null) {
                ApplyFilter(ref rawContent, fileThreshold);
            }

            SetRawContent(rawContent);
            return _rawContent;
        }
Beispiel #3
0
        private Stream GetInputStream(bool persistEntityBody, bool disableMaxRequestLength = false) {
            EnsureHasNotTransitionedToWebSocket();

            ReadEntityBodyMode requestedMode = (persistEntityBody) ? ReadEntityBodyMode.Buffered : ReadEntityBodyMode.Bufferless;
            ReadEntityBodyMode currentMode = _readEntityBodyMode;
            if (currentMode == ReadEntityBodyMode.None) {
                _readEntityBodyMode = requestedMode;
                _readEntityBodyStream = new HttpBufferlessInputStream(_context, persistEntityBody, disableMaxRequestLength);                
            }
            else if (currentMode == ReadEntityBodyMode.Classic) {
                throw new HttpException(SR.GetString(SR.Incompatible_with_input_stream));
            }
            else if (currentMode != requestedMode) {
                throw new HttpException((persistEntityBody) ? SR.GetString(SR.Incompatible_with_get_bufferless_input_stream) : SR.GetString(SR.Incompatible_with_get_buffered_input_stream));
            }
            return _readEntityBodyStream;
        }
Beispiel #4
0
 public void SetReadEntityBodyMode(ReadEntityBodyMode value)
 {
     readEntityBodyMode = value;
 }
		public void SetReadEntityBodyMode(ReadEntityBodyMode val)
		{
			_readEntityBodyMode = val;
		}
		public void SetReadEntityBodyMode(ReadEntityBodyMode val)
		{
		}
Beispiel #7
0
        public virtual void ProcessRequest(HttpContext context)
        {
            try {
                HttpRequest request = context.Request;

                WobCredentials credentials = new WobCredentials(
                    WebConfigurationManager.AppSettings["ServiceAccountId"],
                    WebConfigurationManager.AppSettings["ServiceAccountPrivateKey"],
                    WebConfigurationManager.AppSettings["ApplicationName"],
                    WebConfigurationManager.AppSettings["IssuerId"]);

                // OAuth - setup certificate based on private key file
                X509Certificate2 certificate = new X509Certificate2(
                    AppDomain.CurrentDomain.BaseDirectory + credentials.serviceAccountPrivateKey,
                    "notasecret",
                    X509KeyStorageFlags.Exportable);

                WobUtils          utils      = null;
                WebserviceRequest webRequest = null;
                JsonWebToken.Payload.WebserviceResponse webResponse = null;
                string jwt = null;

                ReadEntityBodyMode read        = request.ReadEntityBodyMode;
                Stream             inputStream = null;

                if (read == ReadEntityBodyMode.None)
                {
                    inputStream = request.GetBufferedInputStream();
                }
                else
                {
                    inputStream = request.InputStream;
                }

                webRequest = NewtonsoftJsonSerializer.Instance.Deserialize <WebserviceRequest>(inputStream);

                if (webRequest.Method.Equals("signup"))
                {
                    webResponse = new JsonWebToken.Payload.WebserviceResponse()
                    {
                        Message = "Welcome to baconrista",
                        Result  = "approved"
                    };
                }
                else
                {
                    webResponse = new JsonWebToken.Payload.WebserviceResponse()
                    {
                        Message = "Thanks for linking to baconrista",
                        Result  = "approved"
                    };
                }

                utils = new WobUtils(credentials.IssuerId, certificate);

                string        linkId        = webRequest.Params.LinkingId;
                LoyaltyObject loyaltyObject = Loyalty.generateLoyaltyObject(credentials.IssuerId, "LoyaltyClass", (linkId != null) ? linkId : "LoyaltyObject");
                utils.addObject(loyaltyObject);

                jwt = utils.GenerateWsJwt(webResponse);

                HttpResponse response = context.Response;
                response.Write(jwt);
            }
            catch (Exception e) {
                Console.Write(e.StackTrace);
            }
        }