Ejemplo n.º 1
0
 static void DataInfoFinished(DownloadDataInfo dataInfo)
 {
     if (dataInfo != null)
     {
         var userIP = dataInfo.UserId;
         var guid   = dataInfo.DataId;
         RemoveDownloadInfoByDataId(guid);
         RefreshUserLimitState(userIP);
     }
 }
Ejemplo n.º 2
0
        private static bool CheckIfUnmodifiedSince(HttpRequest request, DownloadDataInfo dataInfo)
        {
            string   dateHeader;
            DateTime theDate;
            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)
            dateHeader = RetrieveHeader(request, HTTP_HEADER_IF_UNMODIFIED_SINCE, String.Empty);

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


            if (dateHeader.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
                    theDate = DateTime.Parse(dateHeader);

                    // return true if the file was not modified since the indicated date...
                    breturn = dataInfo.LastWriteTimeUtc < theDate;
                }
                catch (Exception)
                {
                    // Converting the indicated date value failed, return false
                    breturn = false;
                }
            }
            return(breturn);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validating partial request, extracts matched ETag
        /// </summary>
        /// <returns>true if pertial request is valid</returns>
        public static bool ValidatePartialRequest(
            HttpRequest request,
            DownloadDataInfo dataInfo,
            out string matchedETag,
            ref int statusCode)
        {
            matchedETag = null;

            if (!(dataInfo.RequestHttpMethod.Equals(HTTP_METHOD_GET) ||
                  dataInfo.RequestHttpMethod.Equals(HTTP_METHOD_HEAD)))
            {
                // Currently, only the GET and HEAD methods
                // are supported...
                statusCode = 501;                  // Not implemented
                return(false);
            }
            else if (!CheckIfModifiedSince(request, dataInfo))
            {
                // The entity is still unmodified...
                statusCode = 304;                  // Not Modified
                return(false);
            }
            else if (!CheckIfUnmodifiedSince(request, dataInfo))
            {
                // The entity was modified since the requested date...
                statusCode = 412;                  // Precondition failed
                return(false);
            }
            else if (!CheckIfMatch(request, dataInfo))
            {
                // The entity does not match the request...
                statusCode = 412;                  // Precondition failed
                return(false);
            }
            else if (!CheckIfNoneMatch(request, ref statusCode, out matchedETag, dataInfo))
            {
                // 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);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adding a new download to the list!
        /// </summary>
        public static void StartNewDownload(DownloadDataInfo dataInfo, string userIP, int userSpeedLimit = 0)
        {
            if (userIP == null || dataInfo == null)
            {
                throw new ArgumentNullException();
            }

            dataInfo.UserId    = userIP;
            dataInfo.Finished += DataInfoFinished;
            lock (_userDownloadInfo)
            {
                _userDownloadInfo.Add(new DownloadInfo {
                    UserIP = userIP, DataInfo = dataInfo, SpeedLimit = userSpeedLimit
                });
            }

            // changing all the downloads speed beglogs to this user
            ApplySpeedLimit(userIP, userSpeedLimit);
        }
Ejemplo n.º 5
0
        private static bool CheckIfModifiedSince(HttpRequest request, DownloadDataInfo dataInfo)
        {
            string   headerDate;
            DateTime theDate;
            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)
            headerDate = RetrieveHeader(request, HTTP_HEADER_IF_MODIFIED_SINCE, string.Empty);

            if (headerDate.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
                    theDate = DateTime.Parse(headerDate);

                    // return true if the file was modified since or at the indicated date...
                    breturn = (dataInfo.LastWriteTimeUtc >= theDate);
                }
                catch
                {
                    // Converting the indicated date value failed, return false
                    breturn = false;
                }
            }
            return(breturn);
        }
Ejemplo n.º 6
0
        private static bool CheckIfMatch(HttpRequest request, DownloadDataInfo responseData)
        {
            string requestHeaderIfMatch;

            string[] entityIDs;
            bool     result = 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)
            requestHeaderIfMatch = RetrieveHeader(request, HTTP_HEADER_IF_MATCH, "*");

            if (requestHeaderIfMatch.Equals("*"))
            {
                // The server may perform the request as if the
                // If-Match header does not exists...
                result = true;
            }
            else
            {
                // One or more Match IDs where sent by the client software...
                entityIDs = requestHeaderIfMatch.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 = entityIDs.GetLowerBound(0); iLoop <= entityIDs.GetUpperBound(0); iLoop++)
                {
                    if (entityIDs[iLoop].Trim().Equals(responseData.EntityTag))
                    {
                        result = true;
                    }
                }
            }
            // return the result...
            return(result);
        }
Ejemplo n.º 7
0
        private static bool CheckIfNoneMatch(
            HttpRequest request,
            ref int statusCode,
            out string matchedETag,
            DownloadDataInfo dataInfo)
        {
            string requestHeaderIfNoneMatch;

            string[] entityIDs;
            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)
            requestHeaderIfNoneMatch = RetrieveHeader(request, HTTP_HEADER_IF_NONE_MATCH, String.Empty);

            if (requestHeaderIfNoneMatch.Equals(String.Empty))
            {
                // Perform the request normally...
                breturn = true;
            }
            else
            {
                if (requestHeaderIfNoneMatch.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...
                    entityIDs = requestHeaderIfNoneMatch.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 = entityIDs.GetLowerBound(0); iLoop <= entityIDs.GetUpperBound(0); iLoop++)
                    {
                        if (entityIDs[iLoop].Trim().Equals(dataInfo.EntityTag))
                        {
                            sreturn = entityIDs[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);
        }
 public DownloadProcess(DownloadDataInfo dataInfo)
 {
     _dataInfo = dataInfo;
 }