Solving CORS problems with reverse proxy - javascript

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.

Related

How to make a GET request to http:localhost from word javascript api?

I am trying to develop a word addin. It will provide corrections for a word based on the context. The language will be Bengali.
My idea is that there will be a python server in localhost. Yes it will run on the localhost. Otherwise if it is a central server there will be too much requests. So somehow (maybe through an exe file I will run the server on in localhost) I will make the server run on localhost. The .exe file can be distributed to anyone and when he/she runs it, it will run on their localhost and then he/she can use the word addin (that I am developing) to call to the localhost to get the desired output. It should be noted that, the platform will be windows.
But the problem is word JavaScript api doesn't let me call to the localhost in http. It only can call to https. But localhost isn't https. If there's anyway I can call the word api in http localhost? Also, as I am looking to make a complete product, I am trying to skip OS related configurations. For example, making localhost certified. Because, when the application (python server) will be transferred, I don''t know how and if it is possible to run script in others' windows OS and make their localhost certified. So, it will be very helpful to make word api just call to my localhost using http (or atmost https://localhost) and not some https://somedomainname.com.
For coding environment to develop the word addin, I am using script lab. So, it will be better to find a solution that will be supported by script lab.
There are a few problems with this approach.
For one - you will never pass the validation, when you're trying to call the localhost - since it will fail by default and you'll be asking the users to install an additional software to support your JS add-in. The add-ins should work standalone.
If there's anyway I can call the word api in http localhost?
No - there isn't in this context. Your final manifest URL's need to be https and they should be externally available.
I am trying to skip OS related configurations. For example, making
localhost certified. Because, when the application (python server)
will be transferred, I don''t know how and if it is possible to run
script in others' windows OS and make their localhost certified.
One of the benefits of the Office store is that Microsoft can look after if the add-in the customers are installing (which we're developing) is malicious or not. Trying to say you want to bypass validation doesn't help this case.
My idea is that there will be a python server in localhost. Yes it
will run on the localhost. Otherwise if it is a central server there
will be too much requests.
It should be a central server and you should handle the scalability aspect of it. You might not have a huge number of users as you're imagining to begin with. I'd recommend Heroku - they support python and their first tier is free, you can develop & publish your add-in and once it starts getting significant traffic, you can look into moving it to other places.

javascript on browser send xmlhttprequest onto loopback server

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.

Requesting HTML page with JavaScript (Angular app)

Python has a module called httplib which allows for the retrieval of an html resource from a URL. With this code:
httpServ = httplib.HTTPConnection("www.google.com")
httpServ.connect()
httpServ.request('GET',"/search?q=python")
...
httpServ.close()
I am trying to do the same thing in my angular app, but using $http get doesn't allow me to retrieve the html document due to the same origin policy.
Is there anything similar to the python method available in JavaScript?
So, the Same-Origin Policy has nothing to do with JavaScript. It basically says "don't allow scripts on a page to talk to scripts being run by another host."
This is an extremely important security feature. It means that if you put jQuery on your page, and somehow a jQuery CDN got hacked and they changed jQuery to send your passwords to another page, it wouldn't work (so long as the browser properly enforces the Same-Origin Policy).
You don't have this problem when working with Python because Python exclusively runs on the server (from a web-app perspective). Your server can talk to any machine it wants to, but browsers do not (and should not as seen above) give that freedom to webpages.
So, how to solve your problem? Make your GET request to a script running on your server. Have your server do a curl or wget or w/e of google.com, then have your server send the data back to the client.

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.

WebSocket Server Javascript Implementation

I have recently discovered HTML 5's support for the WebSocket protocol. I began idly experimenting with it and I decided to undertake a simple chat program project. For it, I'd like to be able write a WebSocket server and have it serve users inside of a blog post, using Google's Blogger website.
The server would be written in Javascript and would have all the code needed to send one person's "conversation line" to all other WebSocket connections connected to it. Later, I may implement "chatrooms" where each line is simply redirected to certain users.
My first question: would it be possible to create a Javascript-based WebSocket server? I've researched a bit, and it seems that all server implementations were in PHP or some server-side language similar to that. Would it be possible to write a WebSocket server with Javascript?
The server implementation would be inside of a webpage, so as long as the blog is up, the server would work as well. My blog can be found here. The client's code would be like this:
server = "http://imdmstromyf8imdcaptomysl.blogspot.com/post_that_handles_chat";
connection = new WebSocket (server);
The problem is, a WebSocket runs on its own protocol (ws:// or wss://), so changing "http://" to "wss://" would not work. Could I tunnel the WebSocket protocol through HTTP? If I did, I would probably have to use Ajax, but avoiding that is the reason I wanted a WebSocket chat program.
Blogger has a place where you can insert your own HTML; would it be possible to use PHP tags to delineate the code from HTML?
I would just like to know whether it's possible to do what I want, and if it is, some implementation tips or (even better) some example code to use.
"The server implementation would be inside of a webpage"
A server implementation is not in a web page. Not sure what you meant here. You may want to edit your question.
Node.js is library written in JavaScript for use on the server. Google this and you should find many ways of getting started on running a WebSockets. To connect to the client, you will be using JavaScript as well, which you should again select a library that supports web sockets.
Additionally, you will need to verify the client' Browser supports it.
You can look at browsers that support web sockets here
Here is a jQuery plugin for web sockets

Categories

Resources