javascript on browser send xmlhttprequest onto loopback server - javascript

so, I've tried to send a javascript XMLHttpRequest on a personally written http server that uses python's core functionality (i.e. socket and regex). took me a while but I finally had it going. so, I tested it out and after debugging the regex for parsing http POST and GET requests, I tested it out through the python console and it worked fine.
in short, the http server receives a GET loopback request with personal identification, and reads whatever text was sent to it as data.
a tts loopback server.
I wanted to do it because asides from selenium that honestly seemed like the only way for me to read random text from the screen using javascript (I could create a browser extension that works alongside it). I already created something for parsing html, so that's not the problem. later I wanted to extend the application and create a GUI using java for generally tts'ing files so I could listen to them while programming.
the problem was that although the socket was bound to port 80 on the loopback interface (127.0.0.1), when I sent an XMLHttpRequest to localhost, the server was not responding. I checked for incoming connections and there were none. from the terminal it worked fine though.

if anyone else is wondering, no, it's not possible. unless it bypasses CORS restriction. (sadly). If anyone wants to do something similar, he has to either bypass CORS restrictions OR if you're building with python you can just use selenium and create a "custom" browser extension.

Related

Solving CORS problems with reverse proxy

I have written a javascript web app for document processing. I now desire to hit some web services such as NLTK-server, TIKA-server, and SOLR, for additional processing.
I am able to hit the REST endpoints of all these using curl, but trying to move to javascript/jquery/Ajax I ran into the same-origin (CORS) problem that many have asked about here. It seems the solution is setting up a reverse proxy, and while I understand the concept, the searches I've done provide results that are way over my head. As common as this problem must be (Web apps hitting web services), I have to think that there must be a simple solution. I'm a developer, not a sys admin and reading how to set up Jetty (For example) as a reverse proxy is an exercise in frustration. Would appreciate if someone could point me to a "Proxy 101" tutorial, as my google-fu has come up short.
**************EDIT************************
I'm developing in Netbeans (I know it's overkill but been using it for years and am comfortable with it) with the embedded GlassFish server. So when executed, I'm running localhost:8383. As for the "Services" I'm hitting, I run:
java -jar tika-server
and it starts up the tika-server listening on port 9998 (also on localhost) and I'm able to submit/retrieve docs using curl. However, when I submit a document to Tika via ajax, I get the CORS error.
I was able to hit the SOLR server, but only because I modified the AJAX request to include:
wt:'json'
because Solr supports JSONP. However, I can not assume that every web service I want to hit supports JSONP. I'm using Tika to extract text from .pdfs, .docx ... I've done so in the past when writing Java code, and just including the jar, but trying to move to a web front end is proving difficult.
You can solve the cors problem with a single line of code. Say you want to enable requests from http://localhost:3000.
java -jar tika-server --cors http://localhost:3000
That's it.

Using Python to communicate with JavaScript?

Is there a way to send data packets from an active Python script to a webpage currently running JavaScript?
The specific usage I'm looking for is to give the ability for the webpage, using JavaScript, to tell the Python script information about the current state of the webpage, then for the Python script to interpret that data and then send data back to the webpage, which the JavaScript then uses to decide which function to execute.
This is for a video game bot (legally), so it would need to happen in real time. I'm fairly proficient in Python and web requests, but I'm just getting into JavaScript, so hopefully a solution for this wouldn't be too complex in terms of Javascript.
EDIT: One way I was thinking to accomplish this would be to have Javascript write to a file that the Python script could also read and write to, but a quick google search says that JavaScript is very limited in terms of file I/O. Would there be a way to accomplish this?
For security reasons, javascript in a browser is usually restricted to only communicate with the site it was loaded from.
Given that, that's an AJAX call, a very standard thing to do.
You can make HTTP requests using the XMLHttpRequest API, which Jquery abstracts with $.ajax and $.get. You can also use the lower level Websockets network API:
https://developer.mozilla.org/en-US/docs/WebSockets
Note that the XMLHttpRequest API will only allow requests to the same server, OR requests that return an appropriate Access-Control-Allow-Origin header.
It sounds like the Javascript is only going to send information, not receive any. In that case, you're in luck. I'm guessing you are also running the Javascript and the Python on the same machine.
Run a Python webserver on the machine the browser is running on. Here's a simple example:
http://webpy.org/install
Once visiting http://127.0.0.1:8080/ in your browser gives the message Hello World!, you can start adding more addresses to your website, for example http://127.0.0.1:8080/report_data, http://127.0.0.1:8080/report_event etc.
Your Javascript can then make AJAX requests using jQuery.ajax or XMLHTTPRequest, to the address http://127.0.0.1:8080/report_data, and pass the information as GET parameters.

Is there a standard for converting a WebSocket request into an HTTP request on the server?

I'm pretty new to WebSockets and this may be completely insane. If so, please let me know what I should be doing instead.
The idea is that I'd like to have a WebSocket connection in the browser that ultimately interfaces with a database. Because the db shouldn't be exposed to the browser there would be, of course, a server layer in between that takes in the WebSocket message and then converts that into something like a POST, or DELETE request. The server would then pass that message along to the database.
So the question is: Is there some kind of standard for translating WebSocket messages into HTTP requests?
I found one blog post where the guy made sure his WebSocket messages from the browser came in the form {"method":"POST","content":"foo"} so they could be understood with normal JSON parsing. Is something like this ok or is there a more acceptable "right way."
Something like this is not only ok, it is the only way. You need a protocol in order to communicate between server and client (otherwise how would a server/client understand what you want from it?). Whatever you choose it will be fine (you can even use standard HTTP over WebSockets). Creating your own protocol (like with the example you came up with) is perfectly fine as well. I prefer JSON-based protocols because it is easy to work with them (JSON parsers are built-in or easily available in most known lanugages)

Can you have a Socket.IO (express) server be loaded from a web browser?

You know, a web server. Right now my Socket.IO server loads from a BATCH file that is a JavaScript file. Can you use node and make the socket.io server load from a web browser. Like a web-server utility tool or something of the sort.
That's explicitly not possible due to the design of WebSockets. It starts as a special HTTP request that, after the handshaking, drops the HTTP protocol and strips it down into the WebSocket protocol -- a nearly bare protocol similar to (but slightly more managed than) raw TCP. Because a web browser specifically cannot handle HTTP requests, it could never initiate the socket as a server.
This was done specifically so it wouldn't be possible to write a drive-by botnet website to use scores of users' computers for DDOS attacks without their knowing, amongst other security concerns.
So it wouldn't surprise me if Flash supported that kind of behavior. ;) (I know Java can, but who enables Java applets?)
I'd say you Can. Not that I can think of a good use case.
You would need to put the startup code somewhere where the web server could run it and you would need to get the web server to return some information to the browser to allow it to then connect. You would also have to insert the socket.io code into the browser after the socket server had started.
So I Think that it would indeed be possible but rather complex for little gain. I suppose one possible use case would be to restart a socket server after failure. Actually I'd do that a slightly different way, probably by calling an external script from Node.
fortunatly the answer is no. if you mean by load / launched , NO. but you can create a script on a server that launch another server once a url is requested by a a client.

How to get reliable HTTP messages via Firefox XPCOM in Javascript

I am trying to program a small server+client in Javascript on Firefox, using XPCOM.
To get the HTTP message in Javascript, I am using the nsIScriptableInputStream interface.
This f**ing component through the read() method randomly cut the message and I cannot make it reliable.
Is anybody know a solution to get reliably the information? (I already tried a binary stream, same failure.)
J.
I had the same problem with unreliability... I ended up using XMLHTTPRequest, which when used from the XPCOM component can do cross site requests. The second part of the docs detail how to instantiate the XPCOM version.
If you're looking to serve HTTP request I'd take a look at the POW source code and the use of server sockets, which implements a basic HTTP server in JavaScript. Also check out httpd.js
If you control the protocol (that is, both the client and server) I would highly recommend using Javascript/JSON for your server-to-client messages. The client can open a stream either via dynamically adding a <script> tag to the DOM. The server can then send a stream of Javascript commands like:
receiveMsg({type:"text", content:"this is my message"});
Then the client just needs to define a receiveMsg function. This allows you to rely on fast browser code to parse the message and determine where the end of each message is, at which point it will call your handler for you.
Even if you're working with an existing HTTP protocol and can't use JSON, is there some reason you can't use XMLHttpRequest? I would expect it to be more stable than some poorly documented Firefox-specific XPCOM interface.
--Chouser

Categories

Resources