java - Is my HTTP protocol design correctly implemented in the coding? -


it's simple client-server application. client sends commands server , server gives output client. however, special concern get command sent server. client request get filename download named file. file gets downloaded client directory http response headers, have designed protocol.

now afraid if coding follows protocol accurately. http response headers line break (in both client , server side).

protocol design:

client:

syntax:  namedfile crlf crlf meaning: downloading named file server representation: text file 

server:

syntax: status: ok crlf length: 20 bytes crlf crlf file contents meaning: file exist in server , ready  download representation: text file 

code:

serverside:

                          .................                           ................. else if (request.startswith("get")) {                         system.out.println("");                         string filename = request.substring(4);                         file file = new file(system.getproperty("user.dir"));                         file[] files = file.listfiles();                          if (fileexists(files, filename)) {                             file = new file(filename);                             int filesize = (int) file.length();                             outputtoclient.print("status ok\r\n"                                     + "size " + filesize + "kb" + "\r\n"                                     + "\r\n"                                     + "file " + filename + " download successfully\r\n");                             outputtoclient.flush();                             // reading files                             fis = new fileinputstream(file);                             os = socket.getoutputstream();                             byte[] buffer = new byte[2^7-1];                             int bytesread = 0;                             while ((bytesread = fis.read(buffer))!= -1) {                                 os.write(buffer, 0, bytesread);                             }                             os.close();                             fis.close();                         } else {                             outputtoclient.print("status 400\r\n"                                     + "file " + filename + " not found\r\n"                                     + "\r\n");                             outputtoclient.flush();                         }                     }                     outputtoclient.flush();                 }                            .................                            ................. 

clientside:

       ............                  ............                 if (request.startswith("get")) {                 file file = new file(request.substring(4));                 = socket.getinputstream();                 fos = new fileoutputstream(file);                  byte[] buffer = new byte[socket.getreceivebuffersize()];                 int bytesreceived = 0;                  while ((bytesreceived = is.read(buffer)) >=0) {                     //while ((bytesreceived = is.read(buffer))>=buffer) {                     fos.write(buffer, 0, bytesreceived);                 }                 request = "";                 fos.close();                 is.close();             }                .................                ................. 

it's not clear me asking, john pointed out- why dont take existing solutions jetty server , adjust taste? give answer: http status codes missing example. , directly "map" server file system isn't best practice.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -