Sneaky way to post to a socket

B

Bill Tschumy

First let me state upfront that I'm a Java programmer, not a web developer.
Consequently I may be flying a bit wild here...

I have scoured the web and usenet for solutions on how to communicate with a
stand-a-alone Java application from a web page. Sounds like there is no
really good browser independent way other than with applets.

Here is what I am trying to do...

I want to load a document with two frames. The top frame basically just has
a button that says something like "Analyze". The bottom frame is initially
loaded up with some web page. The user may browse around via links in the
bottom frame and go to arbitrary web pages which will be loaded in the bottom
frame.

When the user clicks the "Analyze" button, I want to send the URL in the
bottom frame to my Java application for analysis. Both the application and
the web browser are on the same machine. The analysis is really displayed in
the application, so no real results need be displayed in the browser.

I was thinking about using a form post to post to my application (listening
on a special port). I would use a "hidden" form element to send the url for
analysis. This seems to be pretty much working for me. Here is the form
code:

<form action="http://127.0.0.1:7333/results.html" method="post">
<input type="submit" name="foo" value="Analyze Page">
<input type="hidden" name="url_to_parse" value="someurl">
</form>

So I'm posting to the loopback address on port 7333 where my Java app is
listening. The Java app does get data on the port and reads it and generates
some returned text that does get displayed.

My questions:
1. Is there anything fundamentally wrong with doing this this way?

2. Ideally I'd like the post to not change the web browser display, but if I
don't return something the browser says "The page contained no data". Any
way around this?

3. I seem to need to have the form action post to a (bogus) .html file.
Otherwise the browser thinks it needs to download the result as a file. Is
there a better way to handle this?

4. Eventually, I need to build the page that has the forms and I need to
on-the-fly replace the "someurl" with the url loaded in the bottom frame. I
assume I need javascript to do this. Anyone want to post a code snippet to
do what I need (I really know very little about javascript).

Thanks so much for any help or suggestions.
 
J

Jim Ley

My questions:
1. Is there anything fundamentally wrong with doing this this way?

Nope, security makes it difficult.
2. Ideally I'd like the post to not change the web browser display, but if I
don't return something the browser says "The page contained no data". Any
way around this?

return a 204 No Content from your Java App.
4. Eventually, I need to build the page that has the forms and I need to
on-the-fly replace the "someurl" with the url loaded in the bottom frame. I
assume I need javascript to do this. Anyone want to post a code snippet to
do what I need (I really know very little about javascript).

Securirty will stuff you.

onclick="this.form.elements['url']=parent.frames[1].location" in the
submit button, but unless you get some security modifications to the
browser to allow that cross domain scripting it isn't going to work.

Jim.
 
D

Dag Sunde

Bill Tschumy said:
First let me state upfront that I'm a Java programmer, not a web developer.
Consequently I may be flying a bit wild here...

I have scoured the web and usenet for solutions on how to communicate with a
stand-a-alone Java application from a web page. Sounds like there is no
really good browser independent way other than with applets.

Here is what I am trying to do...

I really think a hidden applet in your top frame is the way to go.

The Applet must have allowscripting = true, and a public method that
accepts a string.

Then from your analyze button, you call the applets public method with
the bottom frames URL as a parameter from JavaScript.

As you are primarely a java programmer, you already know how to send
your data from the applet to your java-application on your server.

I want to load a document with two frames. The top frame basically just has
a button that says something like "Analyze". The bottom frame is initially
loaded up with some web page. The user may browse around via links in the
bottom frame and go to arbitrary web pages which will be loaded in the bottom
frame.

When the user clicks the "Analyze" button, I want to send the URL in the
bottom frame to my Java application for analysis. Both the application and
the web browser are on the same machine. The analysis is really displayed in
the application, so no real results need be displayed in the browser.

I was thinking about using a form post to post to my application (listening
on a special port). I would use a "hidden" form element to send the url for
analysis. This seems to be pretty much working for me. Here is the form

<snipped />

/Dag.
 
B

Bill Tschumy

I really think a hidden applet in your top frame is the way to go.

How do you hid the applet. Does it just have zero width and height?
The Applet must have allowscripting = true, and a public method that
accepts a string.

I'm not familiar with any allowscripting property. Is this something in the
applet tag?
Then from your analyze button, you call the applets public method with the
bottom frames URL as a parameter from JavaScript.

Can you give me an example of how to do this? I'm really not a javascript
person.
As you are primarely a java programmer, you already know how to send your
data from the applet to your java-application on your server.

Yes, I can handle this part.

Thanks for your help.
 
B

Bill Tschumy

My questions:
1. Is there anything fundamentally wrong with doing this this way?

Nope, security makes it difficult.
2. Ideally I'd like the post to not change the web browser display, but if
I
don't return something the browser says "The page contained no data". Any
way around this?

return a 204 No Content from your Java App.
4. Eventually, I need to build the page that has the forms and I need to
on-the-fly replace the "someurl" with the url loaded in the bottom frame.
I
assume I need javascript to do this. Anyone want to post a code snippet
to
do what I need (I really know very little about javascript).

Securirty will stuff you.

onclick="this.form.elements['url']=parent.frames[1].location" in the
submit button, but unless you get some security modifications to the
browser to allow that cross domain scripting it isn't going to work.

I feel I am really close on getting this to work. How is this cross domain?
Everything is happening on the local machine. Is this the same as:

onclick="this.myform.url.value=parent.main.location.href"

if the form is defined as:

<form name=myform action="http://127.0.0.1:7333/results.html" method="post">
<input type="submit" name="foo" value="Download source on page" >
<input type="hidden" name="url" value="ffff">
</form>

Does the onclick go in the submit button's input tag?
 
D

Dag Sunde

Bill Tschumy said:
How do you hid the applet. Does it just have zero width and height?

Yes


I'm not familiar with any allowscripting property. Is this something in the
applet tag?

Yes, it's in the applet tag, and its proper name is "mayscript='true'".
Can you give me an example of how to do this? I'm really not a javascript
person.

<script type="text/javascript">

function invokeDispatcher()
{
var javaApplet;
if (typeof document.getElementById('DispatcherApplet').analyzeUrl !=
'undefined')
javaApplet = document.getElementById('DispatcherApplet');
else
return false;

var url = window.top.frames['mainFrame'].location.href;
javaApplet.analyzeUrl(url);
return true;
}

</script>
....
<applet
codebase = "."
code = "com.dagsunde.dispatcher.Dispatcher.class"
id = "DispatcherApplet"
name = "DispatcherApplet"
width = "5"
height = "5"
hspace = "0"
vspace = "0"
align = "left"
mayscript="true"</applet>
<input id="analyzeButton"
type="button"
value="Analyze"
onClick="invokeDispatcher();" />
Yes, I can handle this part.

BUT! Jim Ley is of course right. JS security stops you when the content in
the frame you're trying to analyze origins from a different domain than the
frame with the script is in.

the line:
var url = window.top.frames['mainFrame'].location.href;

from the script above works fine as long as "mainFrame" contains a document
from
the same server, but gives a "Permission denied" if it is ie. from
google.com

You *might* be able to work around this by digitally signing your script
with a
code signing certificate from Thawte or Verisign... (Costs US$400.- a year)

I'll try it later, and inform you...
 
R

Razzbar

Bill Tschumy said:
I feel I am really close on getting this to work. How is this cross domain?
Everything is happening on the local machine. Is this the same as:

Doing it across domains will slam you into the "same origination
policy" I was writing an app that would let people surf in one
window, and bookmark pages in another. But could not even read
the URL of a page that came from a different domain.

Sorry to rain on your parade. The obvious workaround is to have
the user copy and paste the url... but if the page is in a
different frame... you can't always see the url. But you could
use two windows... One where the page to be analyzed is, and
the other where your app is. Copy and paste from one window
to the other. I hated having to do it that way, but got over
it when the app started taking shape.


onclick="this.myform.url.value=parent.main.location.href"

if the form is defined as:

<form name=myform action="http://127.0.0.1:7333/results.html" method="post">
<input type="submit" name="foo" value="Download source on page" >
<input type="hidden" name="url" value="ffff">
</form>

Does the onclick go in the submit button's input tag?

I'd not use a type=submit, but rather a type=button, and in
that tag, do "onclick=myfunction()". The myfunction() function
would validate and submit the form. But that's just my style,
because when you hit 'enter' in any field on a form, it
immediately fires the submit button. So I use a button type
button, then validate, then submit the form.
 
B

Bill Tschumy

Doing it across domains will slam you into the "same origination
policy" I was writing an app that would let people surf in one
window, and bookmark pages in another. But could not even read
the URL of a page that came from a different domain.

Sorry to rain on your parade. The obvious workaround is to have
the user copy and paste the url... but if the page is in a
different frame... you can't always see the url. But you could
use two windows... One where the page to be analyzed is, and
the other where your app is. Copy and paste from one window
to the other. I hated having to do it that way, but got over
it when the app started taking shape.

I appreciate everyone taking time to explain the issues to me. I think I'll
adopt a totally different approach to this.
 
G

Guest

Razzbar said:
Doing it across domains will slam you into the "same origination
policy" I was writing an app that would let people surf in one
window, and bookmark pages in another. But could not even read
the URL of a page that came from a different domain.

Sorry to rain on your parade. The obvious workaround is to have
the user copy and paste the url... but if the page is in a
different frame... you can't always see the url. But you could
use two windows... One where the page to be analyzed is, and
the other where your app is. Copy and paste from one window
to the other. I hated having to do it that way, but got over
it when the app started taking shape.




I'd not use a type=submit, but rather a type=button, and in
that tag, do "onclick=myfunction()". The myfunction() function
would validate and submit the form. But that's just my style,
because when you hit 'enter' in any field on a form, it
immediately fires the submit button. So I use a button type
button, then validate, then submit the form.

couldn't you write a short php script to get the requested info from the
domain instead...then it would be comming from your domain right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top