annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/server/ServerTools.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 package server;
jpayne@68 2
jpayne@68 3 import java.io.BufferedReader;
jpayne@68 4 import java.io.IOException;
jpayne@68 5 import java.io.InputStream;
jpayne@68 6 import java.io.InputStreamReader;
jpayne@68 7 import java.io.OutputStream;
jpayne@68 8 import java.net.HttpURLConnection;
jpayne@68 9 import java.net.InetSocketAddress;
jpayne@68 10 import java.net.MalformedURLException;
jpayne@68 11 import java.net.URL;
jpayne@68 12 import java.net.URLConnection;
jpayne@68 13 import java.util.ArrayList;
jpayne@68 14 import java.util.Arrays;
jpayne@68 15 import java.util.List;
jpayne@68 16 import java.util.Map.Entry;
jpayne@68 17
jpayne@68 18 import com.sun.net.httpserver.Headers;
jpayne@68 19 import com.sun.net.httpserver.HttpExchange;
jpayne@68 20
jpayne@68 21 import fileIO.ReadWrite;
jpayne@68 22 import shared.Tools;
jpayne@68 23 import structures.ByteBuilder;
jpayne@68 24 import structures.StringNum;
jpayne@68 25
jpayne@68 26 public class ServerTools {
jpayne@68 27
jpayne@68 28 public static void main(String[] args){
jpayne@68 29 String address=args[0];
jpayne@68 30 int rounds=1;
jpayne@68 31 String message="";
jpayne@68 32 if(args.length>1){rounds=Integer.parseInt(args[1]);}
jpayne@68 33 if(args.length>2){message=args[2];}
jpayne@68 34 byte[] messageBytes=message.getBytes();
jpayne@68 35
jpayne@68 36 long[] times=new long[rounds];
jpayne@68 37 StringNum response=null;
jpayne@68 38 long prevTime=System.nanoTime();
jpayne@68 39 for(int i=0; i<rounds; i++){
jpayne@68 40 response=sendAndReceive(messageBytes, address);
jpayne@68 41 long currentTime=System.nanoTime();
jpayne@68 42 times[i]=currentTime-prevTime;
jpayne@68 43 prevTime=currentTime;
jpayne@68 44 System.out.println(times[i]);
jpayne@68 45 }
jpayne@68 46
jpayne@68 47 System.out.println(response.s);
jpayne@68 48
jpayne@68 49 Arrays.sort(times);
jpayne@68 50 long sum=Tools.sum(times);
jpayne@68 51 System.out.println("Avg: \t"+sum/1000000.0+" ms");
jpayne@68 52 System.out.println("QPS: \t"+(rounds*1000000000/sum)+" ms");
jpayne@68 53 System.out.println("Median: \t"+(times[rounds/2]/1000000.0)+" ms");
jpayne@68 54
jpayne@68 55 }
jpayne@68 56
jpayne@68 57 public static ByteBuilder readPage(String address, boolean convert){
jpayne@68 58 if(convert){address=PercentEncoding.commonSymbolToCode(address);}
jpayne@68 59 // assert(false) : address;
jpayne@68 60 ByteBuilder bb=new ByteBuilder(256);
jpayne@68 61 boolean success=false;
jpayne@68 62 for(int i=0; i<10 && !success; i++){
jpayne@68 63 try {
jpayne@68 64 URL url=new URL(address);
jpayne@68 65 InputStream is=url.openStream();
jpayne@68 66
jpayne@68 67 byte[] buffer=new byte[4096];
jpayne@68 68 for(int len=is.read(buffer); len>0; len=is.read(buffer)){
jpayne@68 69 bb.append(buffer, 0, len);
jpayne@68 70 }
jpayne@68 71 is.close();
jpayne@68 72 success=true;
jpayne@68 73 } catch (MalformedURLException e) {
jpayne@68 74 e.printStackTrace();
jpayne@68 75 bb.clear();
jpayne@68 76 Tools.pause(1000);
jpayne@68 77 System.err.println("Retrying; attempt "+(i+1)+", URL "+address);
jpayne@68 78 } catch (IOException e) {
jpayne@68 79 e.printStackTrace();
jpayne@68 80 bb.clear();
jpayne@68 81 Tools.pause(1000);
jpayne@68 82 System.err.println("Retrying; attempt "+(i+1)+", URL "+address);
jpayne@68 83 }
jpayne@68 84 }
jpayne@68 85 return bb;
jpayne@68 86 }
jpayne@68 87
jpayne@68 88
jpayne@68 89 /** Send a message to a remote URL, and return the response.
jpayne@68 90 * Set message to null if there is no message. */
jpayne@68 91 public static StringNum sendAndReceive(byte[] message, String address){
jpayne@68 92 address=PercentEncoding.commonSymbolToCode(address);
jpayne@68 93 URL url=null;
jpayne@68 94 InputStream is=null;
jpayne@68 95 HttpURLConnection connection=null;
jpayne@68 96 OutputStream os=null;
jpayne@68 97 try {
jpayne@68 98 url=new URL(address);
jpayne@68 99 connection=(HttpURLConnection) url.openConnection();
jpayne@68 100 connection.setDoOutput(true);
jpayne@68 101 connection.setConnectTimeout(40000); //For testing
jpayne@68 102 os=connection.getOutputStream();
jpayne@68 103 } catch (IOException e1) {
jpayne@68 104 // System.err.println("A:\t"+address+" -> "+url+" -> "+connection+" -> "+os);
jpayne@68 105 // TODO Auto-generated catch block
jpayne@68 106 if(!suppressErrors){e1.printStackTrace();}
jpayne@68 107 }
jpayne@68 108
jpayne@68 109 if(os!=null){
jpayne@68 110 try {
jpayne@68 111 //TODO: It may be useful to set a timeout here.
jpayne@68 112 if(message!=null){os.write(message);}
jpayne@68 113 } catch (IOException e) {
jpayne@68 114 // System.err.println("B:\t"+connection+" -> "+os);
jpayne@68 115 // TODO Auto-generated catch block
jpayne@68 116 if(!suppressErrors){e.printStackTrace();}
jpayne@68 117 }
jpayne@68 118 try {
jpayne@68 119 os.close();
jpayne@68 120 } catch (IOException e) {
jpayne@68 121 // TODO Auto-generated catch block
jpayne@68 122 e.printStackTrace();
jpayne@68 123 }
jpayne@68 124 }
jpayne@68 125
jpayne@68 126 String result=null;
jpayne@68 127 int responseCode=1;
jpayne@68 128 if(connection!=null){
jpayne@68 129 IOException noInputStream=null;
jpayne@68 130 try {
jpayne@68 131 is=connection.getInputStream();
jpayne@68 132 } catch (IOException e) {
jpayne@68 133 noInputStream=e;
jpayne@68 134 }
jpayne@68 135 if(is==null){is=connection.getErrorStream();}
jpayne@68 136
jpayne@68 137 try {
jpayne@68 138 responseCode=connection.getResponseCode();
jpayne@68 139 if(!suppressErrors && (responseCode<200 || responseCode>299)){
jpayne@68 140 System.err.println("Error: Server returned response code "+responseCode);
jpayne@68 141 }
jpayne@68 142 } catch (IOException e) {
jpayne@68 143 if(!suppressErrors) {
jpayne@68 144 e.printStackTrace();
jpayne@68 145 }
jpayne@68 146 }
jpayne@68 147
jpayne@68 148 if(is!=null){
jpayne@68 149 result=readStream(is);
jpayne@68 150 try {
jpayne@68 151 // System.err.println("C:\t"+connection+" -> "+os+" -> "+is+" -> "+(result!=null));
jpayne@68 152 is.close();
jpayne@68 153 } catch (IOException e) {
jpayne@68 154 e.printStackTrace();
jpayne@68 155 }
jpayne@68 156 }else if(noInputStream!=null && !suppressErrors){
jpayne@68 157 noInputStream.printStackTrace();
jpayne@68 158 }
jpayne@68 159 }
jpayne@68 160
jpayne@68 161 return new StringNum(result, responseCode);
jpayne@68 162 }
jpayne@68 163
jpayne@68 164
jpayne@68 165 /** Send a message to a remote URL, and return the response.
jpayne@68 166 * Set message to null if there is no message. */
jpayne@68 167 public static StringNum sendAndReceiveFTP(byte[] message, String address){
jpayne@68 168 address=PercentEncoding.commonSymbolToCode(address);
jpayne@68 169 URL url=null;
jpayne@68 170 InputStream is=null;
jpayne@68 171 URLConnection connection=null;
jpayne@68 172 OutputStream os=null;
jpayne@68 173 try {
jpayne@68 174 url=new URL(address);
jpayne@68 175 connection=url.openConnection();
jpayne@68 176 connection.setDoOutput(true);
jpayne@68 177 connection.setConnectTimeout(40000); //For testing
jpayne@68 178 os=connection.getOutputStream();
jpayne@68 179 } catch (IOException e1) {
jpayne@68 180 // System.err.println("A:\t"+address+" -> "+url+" -> "+connection+" -> "+os);
jpayne@68 181 // TODO Auto-generated catch block
jpayne@68 182 if(!suppressErrors){e1.printStackTrace();}
jpayne@68 183 }
jpayne@68 184
jpayne@68 185 if(os!=null){
jpayne@68 186 try {
jpayne@68 187 //TODO: It may be useful to set a timeout here.
jpayne@68 188 if(message!=null){os.write(message);}
jpayne@68 189 } catch (IOException e) {
jpayne@68 190 // System.err.println("B:\t"+connection+" -> "+os);
jpayne@68 191 // TODO Auto-generated catch block
jpayne@68 192 if(!suppressErrors){e.printStackTrace();}
jpayne@68 193 }
jpayne@68 194 try {
jpayne@68 195 os.close();
jpayne@68 196 } catch (IOException e) {
jpayne@68 197 // TODO Auto-generated catch block
jpayne@68 198 e.printStackTrace();
jpayne@68 199 }
jpayne@68 200 }
jpayne@68 201
jpayne@68 202 String result=null;
jpayne@68 203 // int responseCode=1;
jpayne@68 204 if(connection!=null){
jpayne@68 205 IOException noInputStream=null;
jpayne@68 206 try {
jpayne@68 207 is=connection.getInputStream();
jpayne@68 208 } catch (IOException e) {
jpayne@68 209 noInputStream=e;
jpayne@68 210 }
jpayne@68 211
jpayne@68 212 // try {
jpayne@68 213 //responseCode=connection.getResponseCode();
jpayne@68 214 // if(!suppressErrors && (responseCode<200 || responseCode>299)){
jpayne@68 215 // System.err.println("Error: Server returned response code "+responseCode);
jpayne@68 216 // }
jpayne@68 217 // }
jpayne@68 218 // catch (IOException e) {
jpayne@68 219 // if(!suppressErrors) {
jpayne@68 220 // e.printStackTrace();
jpayne@68 221 // }
jpayne@68 222 // }
jpayne@68 223
jpayne@68 224 if(is!=null){
jpayne@68 225 result=readStream(is);
jpayne@68 226 try {
jpayne@68 227 // System.err.println("C:\t"+connection+" -> "+os+" -> "+is+" -> "+(result!=null));
jpayne@68 228 is.close();
jpayne@68 229 } catch (IOException e) {
jpayne@68 230 e.printStackTrace();
jpayne@68 231 }
jpayne@68 232 }else if(noInputStream!=null && !suppressErrors){
jpayne@68 233 noInputStream.printStackTrace();
jpayne@68 234 }
jpayne@68 235 }
jpayne@68 236
jpayne@68 237 return new StringNum(result, 400);
jpayne@68 238 }
jpayne@68 239
jpayne@68 240 /** Read the body of an incoming HTTP session */
jpayne@68 241 public static String receive(HttpExchange t){
jpayne@68 242 InputStream is=t.getRequestBody();
jpayne@68 243 String s=readStream(is);
jpayne@68 244 return s;
jpayne@68 245 }
jpayne@68 246
jpayne@68 247 /** Completely read an InputStream into a String */
jpayne@68 248 public static String readStream(InputStream is){
jpayne@68 249 if(is==null){return null;}
jpayne@68 250 try {
jpayne@68 251 byte[] buffer=new byte[256];
jpayne@68 252 int count=is.read(buffer);
jpayne@68 253 int next=0;
jpayne@68 254 while(count>-1){
jpayne@68 255 next+=count;
jpayne@68 256 if(next>=buffer.length){
jpayne@68 257 buffer=Arrays.copyOf(buffer, buffer.length*2);
jpayne@68 258 }
jpayne@68 259 count=is.read(buffer, next, buffer.length-next);
jpayne@68 260 }
jpayne@68 261 is.close();
jpayne@68 262 return new String(buffer, 0, next);
jpayne@68 263 } catch (IOException e) {
jpayne@68 264 // TODO Auto-generated catch block
jpayne@68 265 e.printStackTrace();
jpayne@68 266 }
jpayne@68 267 return null;
jpayne@68 268 }
jpayne@68 269
jpayne@68 270 /** Write to the body of an incoming HTTP session */
jpayne@68 271 public static boolean reply(String response, String type, HttpExchange t, boolean verbose, int code, boolean close){
jpayne@68 272 if(verbose){System.err.println("Sending: "+response);}
jpayne@68 273
jpayne@68 274 return reply(response.getBytes(), type, t, false, code, close);
jpayne@68 275 }
jpayne@68 276
jpayne@68 277 /** Write to the body of an incoming HTTP session */
jpayne@68 278 public static boolean replyWithFile(String path, String type, HttpExchange t, boolean verbose, int code, boolean close){
jpayne@68 279 if(verbose){System.err.println("Sending: "+path);}
jpayne@68 280
jpayne@68 281 byte[] response=null;
jpayne@68 282 try {
jpayne@68 283 response=ReadWrite.readRaw(path);
jpayne@68 284 } catch (IOException e1) {
jpayne@68 285 // TODO Auto-generated catch block
jpayne@68 286 e1.printStackTrace();
jpayne@68 287 response=new byte[0];
jpayne@68 288 code=400; //Node sure about this
jpayne@68 289 }
jpayne@68 290
jpayne@68 291 return reply(response, type, t, false, code, close);
jpayne@68 292 }
jpayne@68 293
jpayne@68 294 /** Write to the body of an incoming HTTP session */
jpayne@68 295 public static boolean reply(byte[] response, String type, HttpExchange t, boolean verbose, int code, boolean close){
jpayne@68 296 if(verbose){System.err.println("Sending: "+response);}
jpayne@68 297
jpayne@68 298 {
jpayne@68 299 Headers h = t.getResponseHeaders();
jpayne@68 300 // String type="text/plain";
jpayne@68 301 h.add("Content-Type", type);
jpayne@68 302 }
jpayne@68 303 try {
jpayne@68 304 t.sendResponseHeaders(code, response.length);
jpayne@68 305 OutputStream os = t.getResponseBody();
jpayne@68 306 os.write(response);
jpayne@68 307 os.close();
jpayne@68 308 } catch (IOException e) {
jpayne@68 309 // TODO Auto-generated catch block
jpayne@68 310 e.printStackTrace();
jpayne@68 311 if(close){t.close();}
jpayne@68 312 return false;
jpayne@68 313 }
jpayne@68 314 if(close){t.close();}
jpayne@68 315 return true;
jpayne@68 316 }
jpayne@68 317
jpayne@68 318
jpayne@68 319 /**
jpayne@68 320 * Wait for a set amount of time
jpayne@68 321 * @param millis Time to wait
jpayne@68 322 */
jpayne@68 323 public static void pause(long millis){
jpayne@68 324 String lock=new String("1");
jpayne@68 325 synchronized(lock){
jpayne@68 326 final long time=System.currentTimeMillis()+millis;
jpayne@68 327 while(System.currentTimeMillis()<time){
jpayne@68 328 try {
jpayne@68 329 lock.wait(Tools.max(100, time-System.currentTimeMillis()));
jpayne@68 330 } catch (InterruptedException e) {
jpayne@68 331 // TODO Auto-generated catch block
jpayne@68 332 e.printStackTrace();
jpayne@68 333 }
jpayne@68 334 }
jpayne@68 335 }
jpayne@68 336 }
jpayne@68 337
jpayne@68 338 public static String getClientAddress(HttpExchange t) {
jpayne@68 339
jpayne@68 340 InetSocketAddress client=t.getRemoteAddress();
jpayne@68 341 // InetSocketAddress server=t.getLocalAddress();
jpayne@68 342
jpayne@68 343 //This is for IPv4, class A. Probably extends outside of Berkeley.
jpayne@68 344 String clientAddress=client.toString();
jpayne@68 345 // String ls=server.toString();
jpayne@68 346
jpayne@68 347 if(clientAddress.contains("127.0.0.1")){
jpayne@68 348 Headers clientRequestHeaders=t.getRequestHeaders();
jpayne@68 349 // Headers resh=t.getResponseHeaders();
jpayne@68 350
jpayne@68 351 String xff=clientRequestHeaders.getFirst("X-forwarded-for");
jpayne@68 352 if(xff!=null){clientAddress=xff;}
jpayne@68 353
jpayne@68 354 // System.err.println("\nRequest: ");
jpayne@68 355 // for(Entry<String, List<String>> entry : reqh.entrySet()){
jpayne@68 356 // System.err.println(entry.getKey()+" -> "+entry.getValue());
jpayne@68 357 // }
jpayne@68 358 }
jpayne@68 359 return clientAddress;
jpayne@68 360 }
jpayne@68 361
jpayne@68 362 public static boolean isInternalQuery(HttpExchange t, String prefix, boolean allowLocalHost, boolean printIP, boolean printHeaders){
jpayne@68 363
jpayne@68 364 InetSocketAddress client=t.getRemoteAddress();
jpayne@68 365 InetSocketAddress server=t.getLocalAddress();
jpayne@68 366
jpayne@68 367 if(printIP){System.err.println(client+"\t"+server);}
jpayne@68 368
jpayne@68 369 //This is for IPv4, class A. Probably extends outside of Berkeley.
jpayne@68 370 String clientAddress=client.toString();
jpayne@68 371 String serverAddress=server.toString();
jpayne@68 372
jpayne@68 373 if(clientAddress.contains("127.0.0.1")){//TODO: contains versus startsWith?
jpayne@68 374 Headers requestHeaders=t.getRequestHeaders();
jpayne@68 375
jpayne@68 376 if(printHeaders){
jpayne@68 377 Headers responseHeaders=t.getResponseHeaders();
jpayne@68 378 System.err.println("\nRequest: ");
jpayne@68 379 for(Entry<String, List<String>> entry : requestHeaders.entrySet()){
jpayne@68 380 System.err.println(entry.getKey()+" -> "+entry.getValue());
jpayne@68 381 }
jpayne@68 382 System.err.println("\nResponse: ");
jpayne@68 383 for(Entry<String, List<String>> entry : responseHeaders.entrySet()){
jpayne@68 384 System.err.println(entry.getKey()+" -> "+entry.getValue());
jpayne@68 385 }
jpayne@68 386 }
jpayne@68 387
jpayne@68 388 final String xff=requestHeaders.getFirst("X-forwarded-for");
jpayne@68 389 if(xff!=null){
jpayne@68 390 if(xff.startsWith(prefix)){return true;}
jpayne@68 391 clientAddress=xff;
jpayne@68 392 }else{
jpayne@68 393 return allowLocalHost;
jpayne@68 394 }
jpayne@68 395 }else{
jpayne@68 396 if(clientAddress.startsWith(prefix)){return true;}
jpayne@68 397 }
jpayne@68 398
jpayne@68 399 //Makes sure they match up to the first delimiter
jpayne@68 400 //TODO: This needs to be reviewed
jpayne@68 401 for(int i=0, max=Tools.max(clientAddress.length(), serverAddress.length()); i<max; i++){
jpayne@68 402 char cc=clientAddress.charAt(i), sc=serverAddress.charAt(i);
jpayne@68 403 if(cc!=sc){break;}
jpayne@68 404 if(cc=='.'){//IPv4
jpayne@68 405 return true;
jpayne@68 406 }else if(cc==':'){//IPv6; probably depends on how long the mask is
jpayne@68 407 return true;
jpayne@68 408 }
jpayne@68 409 }
jpayne@68 410
jpayne@68 411 return false;
jpayne@68 412 }
jpayne@68 413
jpayne@68 414 public static ArrayList<String> listDirectory(String baseAddress, final int retries){
jpayne@68 415 // System.err.println("listDirectory '"+baseAddress+"'");
jpayne@68 416 while(baseAddress.endsWith("/")){baseAddress=baseAddress.substring(0, baseAddress.length()-1);}
jpayne@68 417 String baseAddress2=baseAddress.substring(0, baseAddress.lastIndexOf('/')+1);
jpayne@68 418 String address=baseAddress+";type=d";
jpayne@68 419 ArrayList<String> list=new ArrayList<String>();
jpayne@68 420 boolean success=false;
jpayne@68 421 for(int i=Tools.min(0, retries); !success && i<=retries; i++) {
jpayne@68 422 try {
jpayne@68 423 // System.err.println("URL '"+address+"'");
jpayne@68 424 URL url=new URL(address);
jpayne@68 425 URLConnection connection=url.openConnection();
jpayne@68 426 InputStream is=connection.getInputStream();
jpayne@68 427 BufferedReader br=new BufferedReader(new InputStreamReader(is));
jpayne@68 428
jpayne@68 429 String line=null;
jpayne@68 430 while((line=br.readLine())!=null) {
jpayne@68 431 list.add(baseAddress2+line);
jpayne@68 432 }
jpayne@68 433
jpayne@68 434 is.close();
jpayne@68 435 success=true;
jpayne@68 436 } catch (IOException e) {
jpayne@68 437 e.printStackTrace();
jpayne@68 438 if(i<retries){
jpayne@68 439 try {
jpayne@68 440 int x=Tools.mid(30000, i*1000, 1000);
jpayne@68 441 System.err.println("Sleeping for "+x+"ms...");
jpayne@68 442 Thread.sleep(x);
jpayne@68 443 } catch (InterruptedException e1) {
jpayne@68 444 // TODO Auto-generated catch block
jpayne@68 445 e1.printStackTrace();
jpayne@68 446 }
jpayne@68 447 System.err.println("** Retrying ** "+address);
jpayne@68 448 list.clear();
jpayne@68 449 }else{
jpayne@68 450 System.err.println("*** Gave up ***");
jpayne@68 451 }
jpayne@68 452 }
jpayne@68 453 }
jpayne@68 454 return list;
jpayne@68 455 }
jpayne@68 456
jpayne@68 457 public static ArrayList<String> readFTPFile(String address) throws Exception{
jpayne@68 458 address=PercentEncoding.commonSymbolToCode(address);
jpayne@68 459 URL url = new URL(address);
jpayne@68 460 URLConnection conn = url.openConnection();
jpayne@68 461
jpayne@68 462 BufferedReader reader = new BufferedReader(new InputStreamReader(
jpayne@68 463 conn.getInputStream()));
jpayne@68 464 ArrayList<String> list=new ArrayList<String>();
jpayne@68 465 for(String s=reader.readLine(); s!=null; s=reader.readLine()){
jpayne@68 466 list.add(s);
jpayne@68 467 }
jpayne@68 468 return list;
jpayne@68 469 }
jpayne@68 470
jpayne@68 471 /** Don't print caught exceptions */
jpayne@68 472 public static boolean suppressErrors=false;
jpayne@68 473
jpayne@68 474 }