Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class JSONInterpreter : MonoBehaviour
- {
- string loginURL = "https://tarantula.domain.com/home/login";
- string executionsURL = "https://tarantula.domain.com/executions";
- public WWWForm form;
- public Hashtable headers;
- public byte[] rawData;
- public WWW www;
- public string cookie;
- string token;
- bool waiting = false;
- bool connected = false;
- bool loggedIn = false;
- bool hasToken = false;
- bool hasCookie = false;
- bool gotExecutions = false;
- string login = "";
- string password = "";
- void Start()
- {
- form = new WWWForm();
- headers = form.headers;
- headers["Connection"] = "Keep-Alive";
- headers["Host"] = "tarantula.domain.com";
- rawData = form.data;
- }
- bool getToken()
- {
- string htmlString = www.text;
- //Debug.Log("Before cut: " + htmlString);
- int cut = htmlString.IndexOf("authenticity_token\" type=\"hidden\" value=\"");
- if(cut >=0)
- {
- htmlString = htmlString.Substring(cut+41);
- //Debug.Log("First cut: " + htmlString);
- cut = htmlString.IndexOf('"');
- if(cut >=0)
- {
- htmlString = htmlString.Substring(0, cut);
- token = htmlString;
- Debug.Log("Authorisation: " + htmlString);
- return true;
- }
- else
- {
- Debug.Log("Token not found 2");
- return false;
- }
- }
- else
- {
- Debug.Log("Token not found 1");
- return false;
- }
- }
- public int isConnected()
- {
- if(www.isDone)
- {
- if(!connected)
- {
- waiting = false;
- hasToken = getToken();
- if(www.responseHeaders.ContainsKey("Set-Cookie"))
- {
- cookie = www.responseHeaders["Set-Cookie"];
- Debug.Log("recieved cookie: " + cookie);
- //cookie = cookie.Substring(0, cookie.IndexOf(';'));
- //cookie = cookie.Replace("%3D", "=");
- Debug.Log("Cookie: " + cookie);
- hasCookie = true;
- }
- else
- {
- foreach(var header in www.responseHeaders)
- {
- if(header.Value.IndexOf("_tarantula_session") >= 0)
- {
- cookie = header.Value;
- Debug.Log("recieved cookie: " + cookie);
- //cookie = cookie.Substring(0, cookie.IndexOf(';'));
- //cookie = cookie.Replace("%3D", "=");
- Debug.Log("Cookie: " + cookie);
- hasCookie = true;
- }
- }
- }
- Debug.Log("Cookie: " + cookie);
- if(hasToken && hasCookie)
- {
- waiting = true;
- connected = true;
- LogIn(login, password);
- }
- }
- else
- {
- if(www.responseHeaders.ContainsKey("STATUS"))
- {
- if(www.responseHeaders["STATUS"] == "HTTP/1.1 302 Found")
- {
- waiting = false;
- loggedIn = true;
- Debug.Log("YAY");
- }
- else
- {
- Debug.Log(www.responseHeaders["STATUS"]);
- }
- }
- else
- {
- Debug.Log("Response header not found");
- }
- }
- }
- if(waiting)
- {
- return 0;
- }
- if(loggedIn)
- {
- return 1;
- }
- return -1;
- }
- public void Connect(string theLogin, string thePassword)
- {
- login = theLogin;
- password = thePassword;
- www = new WWW(loginURL);
- waiting = true;
- Debug.Log("Connecting");
- LoadData();
- }
- public void LogIn(string theLogin, string thePassword)
- {
- login = theLogin;
- password = thePassword;
- string postString = "utf8=%E2%9C%93&authenticity_token=" + token + "&login=" + login + "&password=" + password;
- byte[] postData = System.Text.Encoding.UTF8.GetBytes(postString);
- headers["Cookie"] = cookie;
- www = new WWW(loginURL, postData, headers);
- waiting = true;
- loggedIn = false;
- Debug.Log("Attempting login");
- LoadData() ;
- }
- IEnumerator LoadData()
- {
- yield return www;
- }
- public GUIContent[] GetExecutions()
- {
- if(www.isDone)
- {
- if(!gotExecutions)
- {
- Debug.Log("Getting executions with cookie : " + cookie);
- headers["Cookie"] = cookie;
- www = new WWW(executionsURL, null, headers);
- gotExecutions = true;
- LoadData();
- }
- else
- {
- Debug.Log(www.text);
- foreach(DictionaryEntry header in headers)
- {
- //Debug.Log("header : " + header.Key + " -: " + header.Value);
- }
- }
- }
- return new GUIContent[0];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement