Esempio n. 1
0
 public HTTPHeader(HTTP_Method method, string url, Dictionary <string, string> fields, Dictionary <string, string> gFields)
 {
     this.httpFields = fields;
     this.Method     = method;
     this.URL        = url;
     this.GetFields  = gFields;
 }
Esempio n. 2
0
 public Action(string name, string title, HTTP_Method method, Uri href, string type = "application/json")
 {
     this.Name = name;
     this.Title = title;
     this.Class.Add(this.Name);
     this.Method = method;
     this.Href = href;
     this.Type = type;
 }
Esempio n. 3
0
        /// <summary>
        /// Gets the signature needed for making requests to 4Over API
        /// </summary>
        /// <param name="M"></param>
        /// <returns></returns>
        public string GetSignature(HTTP_Method M)
        {
            byte[] privateKeyBytes = Encoding.ASCII.GetBytes(PrivateKey);
            byte[] methodBytes     = Encoding.ASCII.GetBytes(M.ToString());

            HashAlgorithm hash = new SHA256CryptoServiceProvider();

            byte[] privateKeyHash = hash.ComputeHash(privateKeyBytes);
            string privateKeyHex  = ByteArrayToString(privateKeyHash); // Should be cb63675c0be505870060bda72ba5e0a80ed76613d7f3781eadb3e90bcb401006

            HMACSHA256 hash2 = new HMACSHA256(Encoding.ASCII.GetBytes(privateKeyHex));

            byte[] signatureBytes = hash2.ComputeHash(methodBytes);

            string signature = ByteArrayToString(signatureBytes); // Should be 998d7f498e35c08f682dda8965daa039af4dd44048998ddd05318f44c959f9da

            return(signature);
        }
Esempio n. 4
0
        public static HTTPHeader ParseRequest(string requestString)
        {
            HTTP_Method method  = HTTP_Method.UNSUPPORTED;
            string      url     = "/";
            double      version = 0;
            Dictionary <string, string> httpFields = new Dictionary <string, string>();
            Dictionary <string, string> getFields  = new Dictionary <string, string>();

            string[] lines        = requestString.Split('\n'); // Take the HTTP request and split it into the seperate lines
            string[] requestParts = lines[0].Split(' ');       // Take line 0 (the request string) and split it into its components
            int      partCounter  = 0;

            for (int i = 0; i < requestParts.Count(); i++)
            {
                string part = requestParts[i];
                if (part == string.Empty)
                {
                    continue;
                }                                       // No place for empty strings

                switch (partCounter)
                {
                case 0:     // Request method
                    try
                    {
                        // Some top kek method selecting
                        method = (HTTP_Method)System.Enum.Parse(typeof(HTTP_Method), part.ToUpper());
                    }
                    catch (Exception ex)
                    {
                        Log.d("Got an unsupported HTTP Request method: {0}", ex.Message);
                    }

                    break;

                case 1:     // Request URL
                    url = HTTPHeader.ExtractURL(part);
                    if (url != part)
                    {
                        getFields = HTTPHeader.ParseGetFields(part);
                    }
                    break;

                case 2:     // Request HTTP Version
                    string part_lower = part.ToLower();
                    if (part_lower.StartsWith("http/") && part.Length >= 5)
                    {
                        try
                        {
                            version = double.Parse(part_lower.Substring(5));
                        }
                        catch (Exception ex)
                        {
                            Log.d("Could not parse HTTP Version: {0}", ex.Message);
                        }
                    }

                    break;

                default:
                    // Well that was awkward
                    break;
                }

                partCounter++;
            }

            if (lines.Length > 1)
            { // We've got ourselves some fields, lets parse those <3
                for (int i = 1; i < lines.Length; i++)
                {
                    string line = lines[i];
                    if (line == string.Empty || line.Length < 3)
                    {
                        continue;
                    }
                    string key         = "";              // Fields aren't that long, so whatever
                    int    keyPosition = 0;
                    for (int c = 0; c < line.Length; c++) // C++, hue hue hue
                    {
                        string chr = line.Substring(c, 1);
                        if (chr == ":")
                        {
                            keyPosition = c; break;
                        }
                    }

                    key = line.Substring(0, keyPosition);
                    if (httpFields.ContainsKey(key))
                    {
                        Log.d("HTTP header contains duplicate field: {0}", key);
                    }
                    else
                    {
                        httpFields[key] = line.Substring(keyPosition + 1);
                    }
                }
            }

            HTTPHeader newHeader = new HTTPHeader(method, url, httpFields, getFields);

            newHeader.Version = version;

            return(newHeader);
        }