Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

JSON Server


  • Please log in to reply
12 replies to this topic

#1 KamakaZ

KamakaZ

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 08 April 2012 - 09:28 AM

Hi,

I am making a Windows Service in delphi XE2. So far I have set a TTCPServer to listen on port 7777, it has access to all data passed through. I am trying to get it to receive data, run a query based on the data passed through, then return a JSON file. To create the JSON file I have to write the headers line by line then the JSON data.

Here is what i have got so far :

HTTP/1.0 200 OK
Content-Type: application/json
Accept: application/json
Content-Length: 294
Content-Type: application/json
Cache-Control: no-cache, must-revalidate
Expires: Mon, 26 Jul 1997 05:00:00 GMT

{"completed_in":0.031,"remote_ip":"202.45.110.174","date_time":"20:45:00 08/04/2
012","number_of_results":2,"query":"SELECT * FROM XUSER","success":1,"error_msg"
:"","results":[{"user_name":"BRAD","web_enabled":1,"first_name":"Bradly"},{"user
_name":"JOHN","web_enabled":0,"first_name":"John"}]}

jQuery Code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Accrivia Test</title>
<script type="text/javascript" src="../jquery-1.7.2.min.js"></script>
</head>

<body>
<input type="text" id="query" value="Twitter" /><button>search</button><br /><br />Results :<hr /><br />
<div id="results">
</div>
<script type="text/javascript">
$(document).ready(function(){
var url='http://brad:7777/search.json?callback=?';
	$('button').click(function(){
		alert("1");
		$.getJSON(url,function(json){
			alert("2");				   
			$.each(json.results,function(i,user){
				alert("3");
			    $("#results").append('<p>'+user.user_name+' - '+user.first_name+'</p>');
			   if (i == 3) return false;
			});
		});
	});
});
</script>
</body>
</html>

It shows alert(1) but none of the others... I have been trying to make it similar to the Twitter search API.

If I open 'http://brad:7777/search.json?callback=?' i get a prompt to download search.json and firebug shows the JSON content in the JSON section and nothing in the response body.
I have tried to write the $.getJSON part in the console but json.* (json.anything) is always undefined.
This is the first one that i have tried cross domain, but from my reading i have to have specified a callback in the URL and it should use JSONP?
The twitter example works no problem and any others i have made (here) that are on a single domain work also.

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:11 AM

Posted 08 April 2012 - 01:17 PM

I am not sure how to explain this exactly, but when you try to use a direct header (like in your firs example), any output to the browser before retrieving that information will kill it. Your 'alert' statement is trying to output to the header, and is therefore killing the response.

For what it is worth, json is just a notation. It can be sent as a plain text string. If your server is using php, you can use 'json_encode($myString)' and send it back to the browser jut like you would a web page. I can try to show you an example later when I am back at my work desk, but this is way easier than you think.

#3 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 08 April 2012 - 07:04 PM

Thanks for the reply, the code does not work even without the alerts though.

The "server" returning the JSON is not a webserver, it's a exe listening on port 7777 and sending lines back to the client.

So it sends 'HTTP/1.1 200 OK' first then 'content-type:application/json' then '' to represent end of the header then the JSON string that I have formatted to match examples on the JSON website, I'm believing JSON is just a string?

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:11 AM

Posted 09 April 2012 - 08:22 AM

Yes, it is just a string formatted to represent a javascript object.

Does the executable have some means of debugging or logging built into it? Where did the exe come from?

#5 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 09 April 2012 - 08:44 AM

Yes it does have debugging, it's written in Delphi XE2

Yes it does have debugging and currently logs to a file in c drive, it's written in Delphi XE2

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:11 AM

Posted 09 April 2012 - 08:52 AM

Are the logs telling you that a connection is even being made?

#7 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 09 April 2012 - 05:45 PM

Yes this is what is in my log file from the above test

GET /accrivia.json?callback=jQuery17208069690119842341_1333889138350&q=?&_=1333889142892 HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive


My service is replicating a web server, so i need to send back what a web server would. I think this is where i am getting stuck, by not sending the right header back?

Edited by KamakaZ, 09 April 2012 - 05:46 PM.

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#8 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 13 April 2012 - 05:17 AM

Has anyone got any ideas on what I could try?

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:11 AM

Posted 13 April 2012 - 06:43 AM

Does that first bit of code represent the entire .exe?

Edited by groovicus, 13 April 2012 - 06:44 AM.


#10 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 16 April 2012 - 05:47 PM

No the first code snippet is the log file, the second is the webpage calling the exe and this is the exe

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
  IdContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,
  Web.Win.Sockets, IdCustomHTTPServer, IdHTTPServer;

type
  TservAccriviaWebService = class(TService)
    TcpServer1: TTcpServer;
    IdHTTPServer1: TIdHTTPServer;
    procedure ServiceExecute(Sender: TService);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient);
    procedure UdpSocket1Receive(Sender: TObject; Buf: PAnsiChar;
      var DataLen: Integer);
  private
    { Private declarations }
    procedure WriteToLogFile(sText : String);
    procedure WriteToJSONFile(sText : String);
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  servAccriviaWebService: TservAccriviaWebService;

const
  sLogFileName : String = 'C:\LogFile.txt';
  sJSONFileName : String = 'D:\Devel\Other\WebService\Win32\Debug\accrivia.json';

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  servAccriviaWebService.Controller(CtrlCode);
end;

function TservAccriviaWebService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TservAccriviaWebService.ServiceExecute(Sender: TService);
var
  scController : TServiceController;
begin
  while not Terminated do begin
    //Comment - Make sure service is kept active

    ServiceThread.ProcessRequests(True);  //Comment - Stops service gracefully
  end;
end;


procedure TservAccriviaWebService.ServiceStart(Sender: TService;
  var Started: Boolean);
begin
  Started := True;
  try
    TCPServer1.Active := True;
    WriteToLogFile('Connected');
  except
    Started := False;
    WriteToLogFile('Filed To Connect');
  end;
  if not TCPServer1.Active then
    Started := False;
end;

procedure TservAccriviaWebService.ServiceStop(Sender: TService;
  var Stopped: Boolean);
begin
  Stopped := True;
end;


procedure TservAccriviaWebService.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
  line,
  returnVal : String;
  a : array[0..9] of byte;
  I : Cardinal;
  sJSON : String;
begin
  line := ' ';

  while ClientSocket.Connected and (Line <> '') do begin
    Line := ClientSocket.Receiveln();

    WriteToLogFile(Line);
    (*

    if Pos('GET /?', Line) > 0 then begin
      ReturnVal := Copy(Line, Pos('GET /?', Line)+6, Pos('&submit', Line)-7);
      ReturnVal := StringReplace(ReturnVal, '+', ' ', [rfReplaceAll, rfIgnoreCase]);
      WriteToLogFile('---');
      WriteToLogFile(ReturnVal);
      WriteToLogFile('---');
    end;
    *)

    (*
    ClientSocket.ReceiveBuf(a,10,0);
    for I := 0 to 8 do begin
      WriteToLogFile(IntToStr(a[I] + a[9]));
    end;
    *)
  end;



  ClientSocket.Sendln('HTTP/1.0 200 OK');
  //ClientSocket.Sendln('Content-disposition: attachment; filename=accrivia_json.json');

  //ClientSocket.Sendln('GET /request HTTP/1.1');
  ClientSocket.Sendln('Accept: application/json');
  //ClientSocket.Sendln('Accept-Encoding: gzip, deflate');
  //ClientSocket.Sendln('Accept-Language: en-us; en');

  //ClientSocket.Sendln('Content-Encoding: identity');
  ClientSocket.Sendln('Content-Length: 294');
  ClientSocket.Sendln('Content-Type: application/json');
  //ClientSocket.Sendln('Access-control-allow-origin: *');
  ClientSocket.Sendln('Cache-Control: no-cache, must-revalidate');
  ClientSocket.Sendln('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  //ClientSocket.Sendln('Content-disposition: attachment; filename=accrivia_json.json');
  //ClientSocket.Sendln('Content-location: ' + ExtractFileName(sJSONFileName);
  //ClientSocket.Sendln('Host: 192.168.1.102');
  //ClientSocket.Sendln('Connection: Close');
  ClientSocket.Sendln('');
  with ClientSocket do begin
    //Sendln('<p>');
    sJSON := '{"completed_in":0.031,"remote_ip":"202.45.110.174","date_time":"20:45:00 08/04/2012","number_of_results":2,"query":"SELECT * FROM XUSER","success":1,"error_msg":"",';
    sJSON := sJSON + '"results":[{"user_name":"BRAD","web_enabled":1,"first_name":"Bradly"},{"user_name":"JOHN","web_enabled":0,"first_name":"John"}]}';
    DeleteFile(sJSONFileName);
    WriteToJSONFile(sJSON);
    Sendln(sJSON);
    //Sendln('</p>');
  end;

  (*
  ClientSocket.Sendln('');
  ClientSocket.Sendln('<h1>Server is busy</h1>');
  ClientSocket.Sendln('<br />');
  ClientSocket.Sendln('<h5>Data Sent:</h5>');
  ClientSocket.Sendln('<p>' + ReturnVal + '</p>');
  *)

  ClientSocket.Close;
end;

procedure TservAccriviaWebService.UdpSocket1Receive(Sender: TObject;
  Buf: PAnsiChar; var DataLen: Integer);
begin
  WriteToLogFile(Buf) ;
end;

procedure TservAccriviaWebService.WriteToLogFile(sText : String);
var
  fLogFile : TextFile;
begin
  try
    AssignFile(fLogFile, sLogFileName);
    if FileExists(sLogFileName) then begin{begin for code skip.}
      Append(fLogFile);
    end else
      Rewrite(fLogFile);
    WriteLn(fLogFile, sText);
  finally
    CloseFile(fLogFile);
  end;
end;

procedure TservAccriviaWebService.WriteToJSONFile(sText : String);
var
  fLogFile : TextFile;
begin
  try
    AssignFile(fLogFile, sJSONFileName);
    if FileExists(sJSONFileName) then begin{begin for code skip.}
      Append(fLogFile);
    end else
      Rewrite(fLogFile);
    WriteLn(fLogFile, sText);
  finally
    CloseFile(fLogFile);
  end;
end;

end.

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:11 AM

Posted 17 April 2012 - 07:53 AM

What does the log file say when you try to connect? Are you developing and testing on the same machine?

#12 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:11 PM

Posted 17 April 2012 - 05:52 PM

The website running the jQuery is on my another PC on my network, the development machine is running the service. If I telnet to brad 7777 I get replies from the service.

This is an axtract from my log file from that last time I was testing.

Connected
GET /search.json?callback=jQuery172037955162189495617_1333893316981&_=1333893319135 HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /favicon.ico HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /favicon.ico HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /favicon.ico HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /search.json?callback=jQuery17207924617428712767_1333893505154&_=1333893522734 HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: null

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: null

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: null

GET /search.json?callback=jQuery1720056824221687296284_1333893649034&_=1333893719411 HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /search.json?callback=jQuery17208587442203567914_1333893768473&_=1333893769777 HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /search.json?callback=jQuery172011982749092292644_1333893798341&_=1333893798383 HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET / HTTP/1.1
Host: brad:7777
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive


There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#13 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:11 AM

Posted 17 April 2012 - 07:24 PM

That doesn't provide any useful information at all, other than header information. If that is your .exe, then you should add a ton of logging. Nowhere in there does it say a successful connection to the service was made.

EDIT: Instead of using the url, use the ip address of the server. Sometimes name resolution on internal systems works a bit goofy.

Edited by groovicus, 17 April 2012 - 07:37 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users