コード例 #1
0
        public HttpResponse processRequest(HttpRequest request)
        {

            // vvv `chrome` (and possibly others) preflights any CORS requests
            if (HttpMethod.OPTIONS == request.Method)
            {
                return buildOptionsResponse(request);
            }
            // ^^^ `chrome` (and possibly others) preflights any CORS requests


            HttpResponse answer = ServicesRequestHandler.processPostRequest(_servicesRegistery, request);

            // vvv without echoing back the 'origin' for CORS requests, chrome (and possibly others) complains "Origin http://localhost:8081 is not allowed by Access-Control-Allow-Origin."
            {
                String origin = request.getHttpHeader("origin");
                if (null != origin)
                {
                    answer.putHeader("Access-Control-Allow-Origin", origin);
                }
            }
            // ^^^ without echoing back the 'origin' for CORS requests, chrome (and possibly others) complains "Origin http://localhost:8081 is not allowed by Access-Control-Allow-Origin."

            return answer;
        }
コード例 #2
0
        private static void setOperationDetailsForRequest(HttpRequest request, String line)
        {
            // If the request line is null, then the other end has hung up on us.  A well
            // behaved client will do this after 15-60 seconds of inactivity.
            if (line == null)
            {
                throw HttpErrorHelper.badRequest400FromOriginator(typeof(HttpRequestReader));
            }


            // HTTP request lines are of the form:
            // [METHOD] [Encoded URL] HTTP/1.?
            string[] tokens = line.Split(new char[] { ' ' });
            if (tokens.Length != 3)
            {
                log.errorFormat("tokens.Length != 3; tokens.Length = {0}; line = '{1}'", tokens.Length, line);
                throw HttpErrorHelper.badRequest400FromOriginator(typeof(HttpRequestReader));
            }

            /*
            * HTTP method ... 
            */
            String method = tokens[0];

            if (HttpMethod.GET.Matches(method))
            {
                request.Method = HttpMethod.GET;
            }
            else if (HttpMethod.POST.Matches(method))
            {
                request.Method = HttpMethod.POST;
            }
			else if (HttpMethod.PUT.Matches(method))
			{
				request.Method = HttpMethod.PUT;
			}
            else if (HttpMethod.OPTIONS.Matches(method))
            {
                request.Method = HttpMethod.OPTIONS;
            }
            else
            {
                log.errorFormat("unknown HTTP method; method = '{0}'; line = '{1}'", method, line);
                throw HttpErrorHelper.methodNotImplemented501FromOriginator(typeof(HttpRequestReader));
            }

            /*
             * HTTP request-uri ...
             */
            String requestUri = tokens[1];
            request.RequestUri = requestUri;

        }
コード例 #3
0
        internal static HttpResponse processPostRequest(ServicesRegistery servicesRegistery, HttpRequest request)
        {

            if (HttpMethod.POST != request.Method)
            {
                log.errorFormat("unsupported method; request.Method = '{0}'", request.Method);
                throw HttpErrorHelper.badRequest400FromOriginator(typeof(ServicesRequestHandler));
            }


            Entity entity = request.Entity;
            if (_MAXIMUM_REQUEST_ENTITY_LENGTH < entity.getContentLength())
            {
                log.errorFormat("_MAXIMUM_REQUEST_ENTITY_LENGTH < entity.getContentLength(); _MAXIMUM_REQUEST_ENTITY_LENGTH = {0}; entity.getContentLength() = {1}", _MAXIMUM_REQUEST_ENTITY_LENGTH, entity.getContentLength());
                throw HttpErrorHelper.requestEntityTooLarge413FromOriginator(typeof(ServicesRequestHandler));
            }

            Data data = GetData(entity);

            BrokerMessage call = Serializer.deserialize(data);
            BrokerMessage response = process(servicesRegistery, call);

            HttpResponse answer;
            {
                if (BrokerMessageType.ONEWAY == call.getMessageType())
                {
                    answer = new HttpResponse(HttpStatus.NO_CONTENT_204);
                }
                else
                {
                    Data responseData = Serializer.Serialize(response);
                    Entity responseBody = new DataEntity(responseData);
                    answer = new HttpResponse(HttpStatus.OK_200, responseBody);
                }
            }

            return answer;


        }
コード例 #4
0
        private Authorization getAuthorizationRequestHeader(HttpRequest request)
        {

            Authorization answer = null;


            String authorization = request.getHttpHeader("authorization");
            if (null == authorization)
            {
                log.error("null == authorization");
                throw HttpErrorHelper.unauthorized401FromOriginator(this );
            }

            answer = Authorization.buildFromString(authorization);

            if (!"auth".Equals(answer.qop))
            {
                log.errorFormat("!\"auth\".Equals(answer.qop); answer.qop = '{0}'", answer.qop);
                throw HttpErrorHelper.unauthorized401FromOriginator(this);
            }

            return answer;
        }
コード例 #5
0
        private HttpResponse buildOptionsResponse(HttpRequest request)
        {
            HttpResponse answer = new HttpResponse(HttpStatus.NO_CONTENT_204);

            // vvv http://www.w3.org/TR/cors/#access-control-allow-methods-response-header
            answer.putHeader("Access-Control-Allow-Methods", "OPTIONS, POST");
            // ^^^ http://www.w3.org/TR/cors/#access-control-allow-methods-response-header


            String accessControlAllowOrigin = request.getHttpHeader("origin");
            if (null == accessControlAllowOrigin)
            {
                accessControlAllowOrigin = "*";
            }
            answer.putHeader("Access-Control-Allow-Origin", accessControlAllowOrigin);

            String accessControlRequestHeaders = request.getHttpHeader("access-control-request-headers");
            if (null != accessControlRequestHeaders)
            {
                answer.putHeader("Access-Control-Allow-Headers", accessControlRequestHeaders);
            }
            
            return answer;
        }
コード例 #6
0
        public HttpResponse processRequest(HttpRequest request)
        {

            RequestHandler httpProcessor;
            {
                String requestUri = request.RequestUri;
                httpProcessor = getHttpProcessor(requestUri);
                if (null == httpProcessor)
                {
                    log.errorFormat("null == httpProcessor; requestUri = {0}", requestUri);
                    throw HttpErrorHelper.notFound404FromOriginator(this);
                }
            }

            DataEntity entity = toDataEntity(request.Entity);

            // we *may* have just consumed the entity data from a stream ... we need to reset the entity in the request
            request.Entity = entity;

            HttpResponse answer = null;
            Authorization authorization = null;

            try
            {
                authorization = getAuthorizationRequestHeader(request);
                _securityManager.authenticateRequest(request.Method.Name, authorization, entity);
                answer = httpProcessor.processRequest(request);
                return answer;
            }
            catch (Exception e)
            {
                log.warn(e.Message);
                answer = HttpErrorHelper.toHttpResponse(e);
                return answer;
            }
            finally
            {
                HttpHeader header = _securityManager.getHeaderForResponse(authorization, answer.Status, answer.Entity);
                answer.putHeader(header.getName(), header.getValue());
            }
        }
コード例 #7
0
        public static HttpRequest readRequest(Stream inputStream)
        {
            MutableData buffer = new MutableData();

            String firstLine = readLine(inputStream, buffer);
            log.debug(firstLine, "firstLine");

            // null corresponds to the end of a stream
            if (null == firstLine)
            {
                return null;
            }

            HttpRequest answer = new HttpRequest();
            setOperationDetailsForRequest(answer, firstLine);

            int i = 0;
            do 
            {
                buffer.clear();
                String line = readLine(inputStream, buffer);
                if (0 == line.Length)
                {
                    break;
                }
                else
                {
                    addHeader(line, answer);
                }

            } while (i < NUMBER_HEADERS_UPPER_BOUND);

            if (i > NUMBER_HEADERS_UPPER_BOUND)
            {
                log.errorFormat("i > NUMBER_HEADERS_UPPER_BOUND; i = {0}", i);
                throw HttpErrorHelper.badRequest400FromOriginator(typeof(HttpRequestReader));
            }

            String contentLengthString = null;

            if (answer.headers.ContainsKey("content-length"))
            {
                contentLengthString = answer.headers["content-length"];
            }

            // no body ?
            if (null == contentLengthString)
            {
                log.debug("null == contentLengthString");
                return answer;
            }


            long contentLength = Int64.Parse(contentLengthString);
            log.debug(contentLength, "contentLength");

            Entity body = new StreamEntity(contentLength, inputStream);
            answer.Entity = body;

            return answer;

        }
コード例 #8
0
        private static void addHeader(String header, HttpRequest request)
        {
         
            String name;
            String value;

            int firstColon = header.IndexOf(":");
            if (-1 == firstColon)
            {

                log.errorFormat("-1 == firstColon; header = '{0}'", header);
                throw HttpErrorHelper.badRequest400FromOriginator(typeof(HttpRequestReader));
            }

            name = header.Substring(0, firstColon).ToLower(); // headers are case insensitive
            value = header.Substring(firstColon + 1).Trim();

            if (Log.isDebugEnabled())
            {
                if ("authorization".Equals(name))
                {
                    log.debug(value, name);
                }
            }
            request.headers[name] = value;

        }
コード例 #9
0
        public HttpResponse processRequest(HttpRequest request)
        {

            String requestUri = request.RequestUri;

            if (requestUri.EndsWith("/"))
            {
                requestUri = requestUri + "index.html";
            }

            { // some validation
                validateRequestUri(requestUri);
                validateMimeTypeForRequestUri(requestUri);
            }

			FileInfo absoluteFileInfo = toAbsoluteFileInfo(requestUri);


			String eTag = getETag(absoluteFileInfo);

			HttpResponse answer;

			String ifNoneMatch = request.getHttpHeader("if-none-match");
			if (null != ifNoneMatch && ifNoneMatch.Equals(eTag))
			{
				answer = new HttpResponse(HttpStatus.NOT_MODIFIED_304);
			}
			else {

				Entity body = readFile(absoluteFileInfo);
				answer = new HttpResponse(HttpStatus.OK_200, body);
				String contentType = MimeTypes.getMimeTypeForPath(requestUri);
				answer.setContentType(contentType);

			}

			if (null != _cacheControl)
			{
				answer.putHeader(_cacheControl.getName(), _cacheControl.getValue());
			}
				

			{
				answer.putHeader("Date", DateTime.UtcNow.ToString("R"));
			}

			{
				var lastModified = absoluteFileInfo.LastWriteTimeUtc;
				answer.putHeader("Last-Modified", lastModified.ToString("R"));
			}


			answer.putHeader("ETag", eTag);
			return answer;

        }
コード例 #10
0
 public HttpResponse processRequest(HttpRequest request)
 {
     return processPostRequest(_servicesRegistery, request);
 }
コード例 #11
0
        public HttpResponse processRequest(HttpRequest request)
        {

            RequestHandler httpProcessor;
            {
                String requestUri = request.RequestUri;
                httpProcessor = GetRequestHandler(requestUri);
                if (null == httpProcessor)
                {
                    log.errorFormat("null == httpProcessor; requestUri = {0}", requestUri);
                    throw HttpErrorHelper.notFound404FromOriginator(this);
                }
            }

            HttpResponse answer = null;
            Authorization authorization = null;

            try
            {
                authorization = getAuthorizationRequestHeader(request);
                _securityManager.authenticateRequest(request.Method.Name, authorization);

                answer = httpProcessor.processRequest(request);
                return answer;
            }
            catch (BaseException e)
            {

                if ( HttpStatus.ErrorDomain.UNAUTHORIZED_401.Equals(e.ErrorDomain))
                {
                    log.warn(e.Message);
                }
                else
                {
                    log.error(e);
                }

                answer = HttpErrorHelper.toHttpResponse(e);
                return answer;
            }
            catch (Exception e)
            {
                log.error(e);
                answer = HttpErrorHelper.toHttpResponse(e);
                return answer;
            }
            finally
            {
                HttpHeader header = _securityManager.getHeaderForResponse(authorization, answer.Status, answer.Entity);
                answer.putHeader(header.getName(), header.getValue());
            }
        }