0

In my mind , the value of private String uri and the value of local var String newURI will be the same in any time.

But in fact, I test many times, sometimes the two values are not the same, why? What happened with NanoHTTPD ?

Guess

I guess the function public Response serve(IHTTPSession session) is muilti threads in the process public class HttpServer extends NanoHTTPD, different threads operate the two value , so sometimes uri and newURL have different value, right?

enter image description here

public class HttpServer extends NanoHTTPD {

   private String uri;          

   private void GetURLAndParValue(IHTTPSession session){        
        uri = session.getUri(); //I pass the value to private var uri               
   }    


   @Override
    public Response serve(IHTTPSession session) {

        GetURLAndParValue(session);               
        String newURI= session.getUri(); // I pass the value to local newURI       

        MResponseInput mResponseInput=new MResponseInput();  

        Utility.LogError("New0:"+this.uri+" : "+newURI);

        String parDiskPath=session.getParms().get(mContext.getString(R.string.ParDiskIndex));
        String parIsAssets=session.getParms().get(mContext.getString(R.string.ParIsAssets));

        Utility.LogError("New1:"+this.uri+" : "+newURI);

        if (newURI.endsWith("/")){

            if (newURI.length()==1) {

                String homePage = mContext.getString(R.string.WebHomePageHtml);
                mResponseInput= GetMResponseInputByHtmlFile(mContext,homePage);

                String s=HttpHelper.HomePage(mContext);
                return newFixedLengthResponse(s);
            }else{                
                String temp=HttpHelper.ListFolderByName(mContext,session);              
                return newFixedLengthResponse(temp);        
            }
        }else{

            String mimeTypeForFile = getMimeTypeForFile(newURI).trim().toLowerCase();
            Utility.LogError("New2:"+this.uri+" : "+newURI);

            if (parIsAssetsValue!=null&&parIsAssetsValue.equals("1")){                       
                return GetResponseInputByAssetsFile(newURI);
            }

            if (mimeTypeForFile.equals("text/html")){
                String fileName=PublicParFun.RemoveFirstBackslash(newURI).toLowerCase();             
            }

        }

        return newFixedLengthResponse(Response.Status.OK,mResponseInput.mimeTypeForFile,mResponseInput.inputStream, mResponseInput.Size);

    }





}
HelloCW
  • 843
  • 22
  • 125
  • 310
  • not sure what you are asking here. can you clarify? – petey Jun 23 '16 at 16:30
  • Why is the value of private var uri different with the value of local var newURI sometimes? – HelloCW Jun 23 '16 at 16:37
  • Why is the value of private var uri different from the value of local var newURI sometimes – HelloCW Jun 23 '16 at 16:44
  • I believe this answer to also fit your question, http://stackoverflow.com/a/2442540/794088 – petey Jun 23 '16 at 17:49
  • Thanks! but stackoverflow.com/a/2442540/794088 don't fit my question, I pass private String uri in the function GetURLAndParValue(IHTTPSession session), there isn't any ambiguity! – HelloCW Jun 24 '16 at 08:06

1 Answers1

0

That's because NanoHTTPD is multithreaded.

Every client socket spawns a thread that keeps track of a specific IHTTPSession, and eventually calls NanoHTTPD#serve(IHTTPSession).

That means that if you receive 2 requests at the same time, NanoHTTPD#serve(IHTTPSession) is called twice concurrently. If you have any knowledge of concurrency then you already know what you're doing wrong.

NanoHTTP was designed so that all you need to do is override NanoHTTPD#serve(IHTTPSession) and you have a webserver working. You are not meant to store session-related data in the server because obviously the server will most likely have more than one session active at any given time.

The way this works will change soon, in v3.0.0, so that it becomes a bit more fool-proof.

LordFokas
  • 68
  • 5