Revision: 8017
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=8017&view=rev
Author:   dnaber
Date:     2012-09-09 21:35:46 +0000 (Sun, 09 Sep 2012)
Log Message:
-----------
HTTP API: new option "--public" to allow access from any host - useful if the 
server get accessed directly from the client via Javascript (not recommended 
for production use)

Modified Paths:
--------------
    trunk/JLanguageTool/CHANGES.txt
    trunk/JLanguageTool/src/main/java/org/languagetool/server/HTTPServer.java
    
trunk/JLanguageTool/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java

Modified: trunk/JLanguageTool/CHANGES.txt
===================================================================
--- trunk/JLanguageTool/CHANGES.txt     2012-09-09 20:48:56 UTC (rev 8016)
+++ trunk/JLanguageTool/CHANGES.txt     2012-09-09 21:35:46 UTC (rev 8017)
@@ -83,6 +83,11 @@
  -HTTP API: small internal cleanup for better exception and charset handling 
(always
   expects UTF-8)
 
+ -HTTP API: the embedded HTTP can now be started with a "--public" parameter so
+  it can be accessed from anywhere (not just localhost). Please be careful 
with this
+  option, it is not recommended for production use yet:
+  java -cp LanguageTool.jar org.languagetool.server.HTTPServer --public
+
  -HTTP API and XML output: extended XML to include the version and build date
   of LanguageTool and the category of each match
 

Modified: 
trunk/JLanguageTool/src/main/java/org/languagetool/server/HTTPServer.java
===================================================================
--- trunk/JLanguageTool/src/main/java/org/languagetool/server/HTTPServer.java   
2012-09-09 20:48:56 UTC (rev 8016)
+++ trunk/JLanguageTool/src/main/java/org/languagetool/server/HTTPServer.java   
2012-09-09 21:35:46 UTC (rev 8017)
@@ -93,7 +93,7 @@
    * Prepare a server on localhost on the given port - use run() to start it. 
The server will bind to localhost.
    * @param verbose if true, the text to be checked will be displayed in case 
of exceptions
    * @param runInternally if true, then the server was started from the GUI.
-   * @param allowedIps the IP addresses from which connections are allowed
+   * @param allowedIps the IP addresses from which connections are allowed or 
<code>null</code> to allow any host
    * @throws PortBindingException if we cannot bind to the given port, e.g. 
because something else is running there
    */
   public HTTPServer(int port, boolean verbose, boolean runInternally, 
Set<String> allowedIps) {
@@ -104,15 +104,19 @@
    * Prepare a server on the given host and port - use run() to start it.
    * @param verbose if true, the text to be checked will be displayed in case 
of exceptions
    * @param runInternally if true, then the server was started from the GUI.
-   * @param host the host to bind to, e.g. <code>"localhost"</code> or 
<code>InetAddress.anyLocalAddress()</code>
-   * @param allowedIps the IP addresses from which connections are allowed
+   * @param host the host to bind to, e.g. <code>"localhost"</code> or 
<code>null</code> to bind to any host
+   * @param allowedIps the IP addresses from which connections are allowed or 
<code>null</code> to allow any host
    * @throws PortBindingException if we cannot bind to the given port, e.g. 
because something else is running there
    * @since 1.7
    */
   public HTTPServer(int port, boolean verbose, boolean runInternally, String 
host, Set<String> allowedIps) {
     this.port = port;
     try {
-      server = HttpServer.create(new InetSocketAddress(host, port), 0);
+      if (host == null) {
+        server = HttpServer.create(new InetSocketAddress(port), 0);
+      } else {
+        server = HttpServer.create(new InetSocketAddress(host, port), 0);
+      }
       server.createContext("/", new LanguageToolHttpHandler(verbose, 
allowedIps, runInternally));
     } catch (Exception e) {
       throw new PortBindingException(
@@ -147,6 +151,7 @@
       System.exit(1);
     }
     boolean verbose = false;
+    boolean publicAccess = false;
     final boolean runInternal = false;
     int port = DEFAULT_PORT;
     for (int i = 0; i < args.length; i++) {
@@ -154,13 +159,19 @@
         port = Integer.parseInt(args[++i]);
       } else if ("-v".equals(args[i]) || "--verbose".equals(args[i])) {
         verbose = true;
+      } else if ("--public".equals(args[i])) {
+        publicAccess = true;
       }
     }
     try {
-      final HttpServer server = HttpServer.create(new 
InetSocketAddress(DEFAULT_HOST, port), 0);
-      server.createContext("/", new LanguageToolHttpHandler(verbose, 
DEFAULT_ALLOWED_IPS, runInternal));
-      server.start();
-      System.out.println("Started LanguageTool HTTP server on " + DEFAULT_HOST 
+ ", port " + port + ".");
+      final HTTPServer server;
+      if (publicAccess) {
+        System.out.println("WARNING: running in public mode, LanguageTool API 
can be accessed without restrictions!");
+        server = new HTTPServer(port, verbose, runInternal, null, null);
+      } else {
+        server = new HTTPServer(port, verbose, runInternal, DEFAULT_HOST, 
DEFAULT_ALLOWED_IPS);
+      }
+      server.run();
     } catch (Exception e) {
       throw new RuntimeException("Could not start LanguageTool HTTP server on 
" + DEFAULT_HOST + ", port " + port, e);
     }

Modified: 
trunk/JLanguageTool/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java
===================================================================
--- 
trunk/JLanguageTool/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java
      2012-09-09 20:48:56 UTC (rev 8016)
+++ 
trunk/JLanguageTool/src/main/java/org/languagetool/server/LanguageToolHttpHandler.java
      2012-09-09 21:35:46 UTC (rev 8017)
@@ -41,9 +41,13 @@
   
   private String[] enabledRules = {};
   private String[] disabledRules = {};
- 
-  LanguageToolHttpHandler(boolean verbose, Set<String> allowedIps,
-                 boolean internal) throws IOException {
+
+  /**
+   * @param verbose print the input text in case of exceptions
+   * @param allowedIps set of IPs that may connect or <tt>null</tt> to allow 
any IP
+   * @throws IOException
+   */
+  LanguageToolHttpHandler(boolean verbose, Set<String> allowedIps, boolean 
internal) throws IOException {
     this.verbose = verbose;
     this.allowedIps = allowedIps;
     this.internalServer = internal;
@@ -58,7 +62,7 @@
       final URI requestedUri = httpExchange.getRequestURI();
       final Map<String, String> parameters = getRequestQuery(httpExchange, 
requestedUri);
       final String remoteAddress = 
httpExchange.getRemoteAddress().getAddress().getHostAddress();
-      if (allowedIps.contains(remoteAddress)) {
+      if (allowedIps == null || allowedIps.contains(remoteAddress)) {
         if (requestedUri.getRawPath().endsWith("/Languages")) {
           // request type: list known languages
           printListOfLanguages(httpExchange);

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Languagetool-cvs mailing list
Languagetool-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to