Exemple #1
0
        void __sendData(string what, string method, byte[] data, string path, tquery query, tnotify notify)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Host + "/" + what);
            request.Credentials = new NetworkCredential(Login, Password);
            request.Method = method;
            request.Timeout = 1000 * 60 * 5;    // 5 min

            request.Accept = "*/*";
            request.ContentType = "application/json";

            if (data != null)
            {
                int cx = data.Length;
                request.ContentLength = cx;

                request.ServicePoint.ConnectionLimit = 1;
                request.ServicePoint.Expect100Continue = true;

                Stream stream = request.GetRequestStream();

                if (cx < bufferSize)
                    stream.Write(data, 0, cx);
                else
                {
                    int bx = 0;
                    while (bx < cx)
                    {
                        int dx = cx - bx;
                        if (dx > bufferSize) dx = bufferSize;

                        stream.Write(data, bx, dx);
                        stream.Flush();

                        bx += dx;
                    }
                }

                stream.Close();
            }
            else
            {
                byte[] buf = new byte[bufferSize];

                using (BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open)))
                {
                    int cx = (int)br.BaseStream.Length;
                    request.ContentLength = cx;
                    Stream stream = request.GetRequestStream();

                    int bx = 0;
                    while (bx < cx)
                    {
                        int dx = cx - bx;
                        if (dx > bufferSize) dx = bufferSize;
                        br.Read(buf, 0, dx);
                        stream.Write(buf, 0, dx);
                        bx += dx;
                    }

                    stream.Close();
                }
            }

            if (request.ContentLength > 0)
            request_json(request, query, notify);

            __endTask(this, null);
        }
Exemple #2
0
 void __postJson(string what, string data, tquery query, tnotify notify)
 {
     __sendData(what,"POST", convert.GetBytes(data),null, query, notify);
 }
Exemple #3
0
 void __request(string what, string method, tquery query, tnotify notify)
 {
     WebRequest request = WebRequest.Create(Host + "/" + what);
     request.Credentials = new NetworkCredential(Login, Password);
     request.Method = method;
     request_json(request, query, notify);
     __endTask(this, null);
 }
Exemple #4
0
 void __get(string what, tquery query, tnotify notify)
 {
     __request(what, "GET", query, notify);
 }
Exemple #5
0
 void __postFile(string what, string path, tquery query, tnotify notify)
 {
     FileInfo inf = new FileInfo(path);
     if (inf.Length >= 0xffff)
         xmap_post(path,query,notify);
     else
         __sendData(what,"POST",  File.ReadAllBytes(path), null, query, notify);
 }
Exemple #6
0
        void xmap_put(string what, string path, tquery query, tnotify notify)
        {
            xmap.Ixmap_auto map = new xmap_auto();

            string response = Path.GetDirectoryName(path) + "\\response";
            if (File.Exists(response)) File.Delete(response);

            int rc;
            map.HttpPut(Host + "/" + what, Login, Password,
                         "application/json", path, response, out rc);

            if (rc == 1)
            if (File.Exists(response))
            using (StreamReader reader = new StreamReader(response))
            {
                fquery = query; Parse(reader);
                request_notify(notify);
            }
        }
Exemple #7
0
 void task_json(StreamReader stream, tquery query)
 {
     fquery = query; Parse(stream);
     __endTask(this, null);
 }
Exemple #8
0
        bool response_json(WebResponse response, tquery query)
        {
            Stream stream = response.GetResponseStream();

            if (stream != null) {
                StreamReader reader = new StreamReader(stream,Encoding.UTF8);
                fquery = query; return Parse(reader);
            }

            return false;
        }
Exemple #9
0
        void request_json(WebRequest request, tquery query, tnotify notify)
        {
            try
            {
                WebResponse response = request.GetResponse();
                response_json(response, query);
                response.Close();

                request_notify(notify);
            }
            catch (WebException e)
            {
                web_error(e,request.Method);
            }
        }
Exemple #10
0
        void curl_post(string path, tquery query, tnotify notify)
        {
            string _auth = Login+":"+Password;
            string _enc = System.Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));

            string dir = Path.GetDirectoryName(path);
            string json = Path.GetFileName(path);

            string bin = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(dir);

            string arguments = "-s" +
                               " -H \"Content-Type: application/json\"" +
                               " -H \"Authorization: Basic " + _enc + "\"" +
                               " -X POST --data-binary @" + json +
                               " " + Host + "/" + "commit";

            __message("curl.exe", "commit...");

            try
            {
                Process proc = new Process();
                proc.StartInfo.FileName = bin + "\\curl.exe";
                proc.StartInfo.Arguments = arguments;

                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.CreateNoWindow = true;

                proc.Start();

                string ack = dir + "\\response";
                if (File.Exists(ack)) File.Delete(ack);

                using (StreamWriter sw = new StreamWriter(ack))
                {

                    while (!proc.StandardOutput.EndOfStream)
                    {
                        string line = proc.StandardOutput.ReadLine();
                        sw.WriteLine(line);
                    }

                    sw.Close();
                }

                if (File.Exists(ack))
                using (StreamReader reader = new StreamReader(ack))
                {
                    fquery = query; Parse(reader);
                    request_notify(notify);
                }
            }
            catch (Exception e)
            {
                __message("curl", e.Message);
            }

            Directory.SetCurrentDirectory(bin);
        }