Exemple #1
0
        public static string CreateRequest(EnumRequestType requestType, string methodName, string parameter)
        {
            string serviceName = string.Empty;

            switch (requestType)
            {
            case EnumRequestType.GetSensorsInfo:
                serviceName = GetAppConfig("GetSensorsInfo");
                break;

            case EnumRequestType.UpLoadData:
                serviceName = GetAppConfig("UpLoadData");
                break;
            }
            RestClient client = new RestClient(GetAppConfig("iCMSServer") + "/" + serviceName);

            return(client.Post(parameter, methodName));
        }
        // returns the start position of the unused bytes
        public static void LoadHeader(byte[] rawRequest, int rawRequestSize, out int headerLength, out Hashtable header, out EnumRequestType requestType)
        {
            header = new Hashtable();
            requestType = GetRequestType(ref rawRequest);
            string requestString = new string(Encoding.UTF8.GetChars(rawRequest, 0, rawRequestSize));
            headerLength = GetHeaderLength(ref rawRequest);
            string[] headerArray = requestString.Split('\n');
            if (headerArray.Length > 1)
            {
                string[] split = headerArray[0].Split(' ');
                if (split.Length >= 2)
                {
                    // first line GET /index.html?userid=joe&password=guessme HTTP/1.1
                    header.Add("Uri", split[1]);
                }
                if (split.Length >= 3)
                {
                    header.Add("HttpVersion", split[2]);
                }

            }
            for (int i = 1; i < headerArray.Length; i++)
            {
                AddFieldToHashTable(header, headerArray[i]);
            }
        }
        //POST /login.jsp HTTP/1.1
        //Host: www.mysite.com
        //User-Agent: Mozilla/4.0
        //Content-Length: 27
        //Content-Type: application/x-www-form-uriencoded
        //userid=joe&password=guessme
        //or also
        //Content-Type: application/octet-stream for binary
        public PostRequest(byte[] bRawRequest, int contentStart, int contentSize, Hashtable headerFields, EnumRequestType requestType, Socket connectionSocket, GetMoreBytesHandler getMoreBytesHandler)
            : base(bRawRequest, contentStart, contentSize, headerFields, requestType)
        {
            ContentTypeGroup = EnumContentType.Text;// todo check this maybe remove
            GetMoreBytesHandler = getMoreBytesHandler;
            ConnectionSocket = connectionSocket;
            ContentStartPosition = contentStart;
            ContentLength = contentSize;
            // pull out uri
            Uri = headerFields["Uri"].ToString();
            ContentType = HeaderFields["Content-Type"].ToString();
            if(HeaderFields.Contains("Content-Length"))
            {
                int contentLengthFromHeader = int.Parse(HeaderFields["Content-Length"].ToString());
                if( contentLengthFromHeader != ContentLength)
                {
                    ; // means more packets needed
                }
            }

            if (ContentTypeGroup == EnumContentType.Text)
            {
                string content = new string(Encoding.UTF8.GetChars(bRawRequest, ContentStartPosition, ContentLength - ContentStartPosition));
                //ContentVariables = GetEncodedVariables(content);
            }

            //if (ContentType.ToLower().IndexOf("multipart/form-data") >= 0)
            //{    //    http://www.4guysfromrolla.com/webtech/091201-1.shtml check filesize before uploading
            //    byte[] boundary = null;// todo get this.
            //    int boundaryLength = -1;
            //    int[] boundaryDelimiterPositions = null;
            //    ContentTypeGroup = EnumContentType.MultipartFormData;
            //    GetMultipartFormTextVariables(buffer, ContentStartPosition, boundary, out boundaryDelimiterPositions, out boundaryLength);
            //    ContentVariables = GetMultipartFormTextVariables(buffer, boundaryDelimiterPositions, boundaryLength);
            //    ContentVariablesBinary = GetMultipartFormBinaryVariables(buffer, boundaryDelimiterPositions, boundaryLength);
            //}
            //else
            //{
            //    if (ContentType.ToLower().IndexOf("application/octet-stream") >= 0)
            //    {
            //        ContentTypeGroup = EnumContentType.Binary;
            //        throw new NotImplementedException("application/octet-stream not implemented.");
            //    }
            //    else
            //    {
            //        ContentTypeGroup = EnumContentType.Text;
            //        //Content-Type: application/x-www-form-urlencoded Todo are there any other options?
            //        string content = new string(Encoding.UTF8.GetChars(buffer, ContentStartPosition, dataLength - ContentStartPosition));
            //        ContentVariables = GetEncodedVariables(content);
            //    }
            //}
        }
 public BaseRequest(byte[] bRawRequest, int contentStart, int contentSize, Hashtable headerFields, EnumRequestType requestType)
 {
     LoadBaseRequest(ref bRawRequest, contentStart, contentSize, headerFields, requestType);
 }
        protected void LoadBaseRequest(ref byte[] bRawRequest, int contentStart, int contentSize, Hashtable headerFields, EnumRequestType requestType)
        {
            HttpRequestType = requestType;
            HeaderFields = headerFields;
            // Convert to string, will include HTTP headers.
            string content = new string(Encoding.UTF8.GetChars(bRawRequest, contentStart, contentSize));// there shouldn't be content

            // pull out uri
            Uri = headerFields["Uri"].ToString();// url is deprecated?
            string baseUri = "";
            QuerystringVariables = GetQueryStringVariables(Uri, out baseUri);
            BaseUri = baseUri;
            //parse uri for command processing.
            //start with the 2nd character to avoid a blank entry.
            //            ParsedUri = Uri.Substring(1).Split(new char[] { '/' });
        }