Example #1
0
        private static bool CheckIfUnmodifiedSince(HttpRequest request, ResumableResponseData responseData)
        {
            string   sDate;
            DateTime dDate;
            bool     breturn;


            // Checks the If-Unmodified or Unless-Modified-Since header, if
            // one of them was sent with the request.
            //
            // returns true, if the file was not modified since the
            //               indicated date (RFC 1123 format), or
            //               if no header was sent,
            // returns false, if the file was modified since the indicated date


            // Retrieve If-Unmodified-Since Header value from Request (Empty if none is indicated)
            sDate = RetrieveHeader(request, HTTP_HEADER_IF_UNMODIFIED_SINCE, String.Empty);

            if (sDate.Equals(String.Empty))
            {
                // If-Unmodified-Since was not sent, check Unless-Modified-Since...
                sDate = RetrieveHeader(request, HTTP_HEADER_UNLESS_MODIFIED_SINCE, String.Empty);
            }


            if (sDate.Equals(String.Empty))
            {
                // No date was indicated,
                // so just give this as true
                breturn = true;
            }

            else
            {
                try
                {
                    // ... to parse the indicated sDate to a datetime value
                    dDate = DateTime.Parse(sDate);
                    // return true if the file was not modified since the indicated date...
                    breturn = responseData.LastWriteTimeUTC < DateTime.Parse(sDate);
                }
                catch (Exception)
                {
                    // Converting the indicated date value failed, return false
                    breturn = false;
                }
            }
            return(breturn);
        }
Example #2
0
        public static bool ValidatePartialRequest(HttpRequest request, ResumableResponseData responseData, out string matchedETag, ref int statusCode)
        {
            const string HTTP_METHOD_GET  = "GET";
            const string HTTP_METHOD_HEAD = "HEAD";

            matchedETag = null;

            if (!(responseData.HttpMethod.Equals(HTTP_METHOD_GET) ||
                  responseData.HttpMethod.Equals(HTTP_METHOD_HEAD)))
            {
                // Currently, only the GET and HEAD methods
                // are supported...
                statusCode = 501;                  // Not implemented
                return(false);
            }
            else if (!CheckIfModifiedSince(request, responseData))
            {
                // The entity is still unmodified...
                statusCode = 304;                  // Not Modified
                return(false);
            }
            else if (!CheckIfUnmodifiedSince(request, responseData))
            {
                // The entity was modified since the requested date...
                statusCode = 412;                  // Precondition failed
                return(false);
            }
            else if (!CheckIfMatch(request, responseData))
            {
                // The entity does not match the request...
                statusCode = 412;                  // Precondition failed
                return(false);
            }
            else if (!CheckIfNoneMatch(request, ref statusCode, out matchedETag, responseData))
            {
                // The entity does match the none-match request, the response
                // code was set inside the CheckifNoneMatch function

                //matchedETag

                // valid but content is not required
                return(false);
            }
            else
            {
                //valid
                return(true);
            }
        }
Example #3
0
        private static bool CheckIfModifiedSince(HttpRequest request, ResumableResponseData responseData)
        {
            string   sDate;
            DateTime dDate;
            bool     breturn;

            // Checks the If-Modified header if it was sent with the request.
            //
            // returns true, if the file was modified since the
            //               indicated date (RFC 1123 format), or
            //               if no header was sent,
            // returns false, if the file was not modified since
            //                the indicated date


            // Retrieve If-Modified-Since Header value from Request (Empty if none is indicated)
            sDate = RetrieveHeader(request, HTTP_HEADER_IF_MODIFIED_SINCE, string.Empty);

            if (sDate.Equals(String.Empty))
            {
                // No If-Modified-Since date was indicated,
                // so just give this as true
                breturn = true;
            }

            else
            {
                try
                {
                    // ... to parse the indicated sDate to a datetime value
                    dDate = DateTime.Parse(sDate);
                    // return true if the file was modified since or at the indicated date...
                    breturn = (responseData.LastWriteTimeUTC >= DateTime.Parse(sDate));
                }
                catch
                {
                    // Converting the indicated date value failed, return false
                    breturn = false;
                }
            }
            return(breturn);
        }
Example #4
0
        private static bool CheckIfMatch(HttpRequest request, ResumableResponseData responseData)
        {
            string sRequestHeaderIfMatch;

            string[] sEntityIDs;
            bool     breturn = false;

            // Checks the If-Match header if it was sent with the request.
            //
            // returns true if one of the header values matches the file//s entity tag,
            //              or if no header was sent,
            // returns false if a header was sent, but does not match the file.


            // Retrieve If-Match Header value from Request (*, meaning any, if none is indicated)
            sRequestHeaderIfMatch = RetrieveHeader(request, HTTP_HEADER_IF_MATCH, "*");

            if (sRequestHeaderIfMatch.Equals("*"))
            {
                // The server may perform the request as if the
                // If-Match header does not exists...
                breturn = true;
            }
            else
            {
                // One or more Match IDs where sent by the client software...
                sEntityIDs = sRequestHeaderIfMatch.Replace("bytes=", "").Split(",".ToCharArray());

                // Loop through all entity IDs, finding one
                // which matches the current file's ID will
                // be enough to satisfy the If-Match
                for (int iLoop = sEntityIDs.GetLowerBound(0); iLoop <= sEntityIDs.GetUpperBound(0); iLoop++)
                {
                    if (sEntityIDs[iLoop].Trim().Equals(responseData.EntityTag))
                    {
                        breturn = true;
                    }
                }
            }
            // return the result...
            return(breturn);
        }
Example #5
0
 public ResumableResponse(ResumableResponseData responseData)
 {
     _responseData = responseData;
 }
Example #6
0
 public ResumableResponse(ResumableResponseData responseData)
 {
     _responseData = responseData;
 }
Example #7
0
        private static bool CheckIfNoneMatch(HttpRequest request, ref int statusCode, out string matchedETag, ResumableResponseData responseData)
        {
            string sRequestHeaderIfNoneMatch;

            string[] sEntityIDs;
            bool     breturn = true;
            string   sreturn = "";

            matchedETag = "";
            // Checks the If-None-Match header if it was sent with the request.
            //
            // returns true if one of the header values matches the file//s entity tag,
            //              or if "*" was sent,
            // returns false if a header was sent, but does not match the file, or
            //               if no header was sent.

            // Retrieve If-None-Match Header value from Request (*, meaning any, if none is indicated)
            sRequestHeaderIfNoneMatch = RetrieveHeader(request, HTTP_HEADER_IF_NONE_MATCH, String.Empty);

            if (sRequestHeaderIfNoneMatch.Equals(String.Empty))
            {
                // Perform the request normally...
                breturn = true;
            }
            else
            {
                if (sRequestHeaderIfNoneMatch.Equals("*"))
                {
                    // The server must not perform the request
                    statusCode = 412;                      // Precondition failed
                    breturn    = false;
                }
                else
                {
                    // One or more Match IDs where sent by the client software...
                    sEntityIDs = sRequestHeaderIfNoneMatch.Replace("bytes=", "").Split(",".ToCharArray());

                    // Loop through all entity IDs, finding one which
                    // does not match the current file//s ID will be
                    // enough to satisfy the If-None-Match
                    for (int iLoop = sEntityIDs.GetLowerBound(0); iLoop <= sEntityIDs.GetUpperBound(0); iLoop++)
                    {
                        if (sEntityIDs[iLoop].Trim().Equals(responseData.EntityTag))
                        {
                            sreturn = sEntityIDs[iLoop];
                            breturn = false;
                        }
                    }

                    if (!breturn)
                    {
                        // One of the requested entities matches the current file's tag,
                        //objResponse.AppendHeader("ETag", sreturn);
                        matchedETag = sreturn;
                        statusCode  = 304;                        // Not Modified
                    }
                }
            }
            // return the result...
            return(breturn);
        }
Example #8
0
        private static bool CheckIfUnmodifiedSince(HttpRequest request, ResumableResponseData responseData)
        {
            string sDate;
            DateTime dDate;
            bool breturn;

            // Checks the If-Unmodified or Unless-Modified-Since header, if
            // one of them was sent with the request.
            //
            // returns true, if the file was not modified since the
            //               indicated date (RFC 1123 format), or
            //               if no header was sent,
            // returns false, if the file was modified since the indicated date

            // Retrieve If-Unmodified-Since Header value from Request (Empty if none is indicated)
            sDate = RetrieveHeader(request, HTTP_HEADER_IF_UNMODIFIED_SINCE, String.Empty);

            if (sDate.Equals(String.Empty))
                // If-Unmodified-Since was not sent, check Unless-Modified-Since...
                sDate = RetrieveHeader(request, HTTP_HEADER_UNLESS_MODIFIED_SINCE, String.Empty);

            if (sDate.Equals(String.Empty))
                // No date was indicated,
                // so just give this as true
                breturn = true;

            else
            {
                try
                {
                    // ... to parse the indicated sDate to a datetime value
                    dDate = DateTime.Parse(sDate);
                    // return true if the file was not modified since the indicated date...
                    breturn = responseData.LastWriteTimeUTC < DateTime.Parse(sDate);
                }
                catch (Exception)
                {
                    // Converting the indicated date value failed, return false
                    breturn = false;
                }
            }
            return breturn;
        }
Example #9
0
        private static bool CheckIfNoneMatch(HttpRequest request, ref int statusCode, out string matchedETag, ResumableResponseData responseData)
        {
            string sRequestHeaderIfNoneMatch;
            string[] sEntityIDs;
            bool breturn = true;
            string sreturn = "";
            matchedETag = "";
            // Checks the If-None-Match header if it was sent with the request.
            //
            // returns true if one of the header values matches the file//s entity tag,
            //              or if "*" was sent,
            // returns false if a header was sent, but does not match the file, or
            //               if no header was sent.

            // Retrieve If-None-Match Header value from Request (*, meaning any, if none is indicated)
            sRequestHeaderIfNoneMatch = RetrieveHeader(request, HTTP_HEADER_IF_NONE_MATCH, String.Empty);

            if (sRequestHeaderIfNoneMatch.Equals(String.Empty))
                // Perform the request normally...
                breturn = true;
            else
            {
                if (sRequestHeaderIfNoneMatch.Equals("*"))
                {
                    // The server must not perform the request
                    statusCode = 412;  // Precondition failed
                    breturn = false;
                }
                else
                {
                    // One or more Match IDs where sent by the client software...
                    sEntityIDs = sRequestHeaderIfNoneMatch.Replace("bytes=", "").Split(",".ToCharArray());

                    // Loop through all entity IDs, finding one which
                    // does not match the current file//s ID will be
                    // enough to satisfy the If-None-Match
                    for (int iLoop = sEntityIDs.GetLowerBound(0); iLoop <= sEntityIDs.GetUpperBound(0); iLoop++)
                    {
                        if (sEntityIDs[iLoop].Trim().Equals(responseData.EntityTag))
                        {
                            sreturn = sEntityIDs[iLoop];
                            breturn = false;
                        }
                    }

                    if (!breturn)
                    {
                        // One of the requested entities matches the current file's tag,
                        //objResponse.AppendHeader("ETag", sreturn);
                        matchedETag = sreturn;
                        statusCode = 304; // Not Modified
                    }
                }
            }
            // return the result...
            return breturn;
        }
Example #10
0
        private static bool CheckIfModifiedSince(HttpRequest request, ResumableResponseData responseData)
        {
            string sDate;
            DateTime dDate;
            bool breturn;

            // Checks the If-Modified header if it was sent with the request.
            //
            // returns true, if the file was modified since the
            //               indicated date (RFC 1123 format), or
            //               if no header was sent,
            // returns false, if the file was not modified since
            //                the indicated date

            // Retrieve If-Modified-Since Header value from Request (Empty if none is indicated)
            sDate = RetrieveHeader(request, HTTP_HEADER_IF_MODIFIED_SINCE, string.Empty);

            if (sDate.Equals(String.Empty))
                // No If-Modified-Since date was indicated,
                // so just give this as true
                breturn = true;

            else
            {
                try
                {
                    // ... to parse the indicated sDate to a datetime value
                    dDate = DateTime.Parse(sDate);
                    // return true if the file was modified since or at the indicated date...
                    breturn = (responseData.LastWriteTimeUTC >= DateTime.Parse(sDate));
                }
                catch
                {
                    // Converting the indicated date value failed, return false
                    breturn = false;
                }
            }
            return breturn;
        }
Example #11
0
        private static bool CheckIfMatch(HttpRequest request, ResumableResponseData responseData)
        {
            string sRequestHeaderIfMatch;
            string[] sEntityIDs;
            bool breturn = false;

            // Checks the If-Match header if it was sent with the request.
            //
            // returns true if one of the header values matches the file//s entity tag,
            //              or if no header was sent,
            // returns false if a header was sent, but does not match the file.

            // Retrieve If-Match Header value from Request (*, meaning any, if none is indicated)
            sRequestHeaderIfMatch = RetrieveHeader(request, HTTP_HEADER_IF_MATCH, "*");

            if (sRequestHeaderIfMatch.Equals("*"))
                // The server may perform the request as if the
                // If-Match header does not exists...
                breturn = true;
            else
            {
                // One or more Match IDs where sent by the client software...
                sEntityIDs = sRequestHeaderIfMatch.Replace("bytes=", "").Split(",".ToCharArray());

                // Loop through all entity IDs, finding one
                // which matches the current file's ID will
                // be enough to satisfy the If-Match
                for (int iLoop = sEntityIDs.GetLowerBound(0); iLoop <= sEntityIDs.GetUpperBound(0); iLoop++)
                {
                    if (sEntityIDs[iLoop].Trim().Equals(responseData.EntityTag))
                        breturn = true;
                }
            }
            // return the result...
            return breturn;
        }
Example #12
0
        public static bool ValidatePartialRequest(HttpRequest request, ResumableResponseData responseData, out string matchedETag, ref int statusCode)
        {
            const string HTTP_METHOD_GET = "GET";
            const string HTTP_METHOD_HEAD = "HEAD";
            matchedETag = null;

            if (!(responseData.HttpMethod.Equals(HTTP_METHOD_GET) ||
                responseData.HttpMethod.Equals(HTTP_METHOD_HEAD)))
            {
                // Currently, only the GET and HEAD methods
                // are supported...
                statusCode = 501;  // Not implemented
                return false;
            }
            else if (!CheckIfModifiedSince(request, responseData))
            {
                // The entity is still unmodified...
                statusCode = 304;  // Not Modified
                return false;
            }
            else if (!CheckIfUnmodifiedSince(request, responseData))
            {
                // The entity was modified since the requested date...
                statusCode = 412;  // Precondition failed
                return false;
            }
            else if (!CheckIfMatch(request, responseData))
            {
                // The entity does not match the request...
                statusCode = 412;  // Precondition failed
                return false;
            }
            else if (!CheckIfNoneMatch(request, ref statusCode, out matchedETag, responseData))
            {
                // The entity does match the none-match request, the response
                // code was set inside the CheckifNoneMatch function

                //matchedETag

                // valid but content is not required
                return false;
            }
            else
            {
                //valid
                return true;
            }
        }