How a server differs between two requests? - web

Can someone please explain how the server differs between the two requests www.google.com/maps and www.google.com/inbox?

It's best for you to see for yourself. If you're on Linux/MacOs, go to a shell and type nc -l 8080. Then open a web browser and look at, say, http://localhost:8080/a/b/c/d.
The nc command listens on port 8080 and just prints out whatever it receives. Here it's serving as a bad web browser that listens for requests but has no idea how to respond.
You'll see that the first two lines of what it receives are:
GET /a/b/c/d HTTP/1.1
Host: localhost:8080
followed by other information the browser things the server might need.

Related

How to implement a "Command line interface server"

If I were creating a webserver then there would be one thread listening to port 8080 and every time there was a new HTTP request then it would trigger some logic in the server. I want to implement the same but instead of listening on port 8080 for HTTP requests, then it would listen for commands from the local operating system (OS) on a command-line interface. These commands can come from any terminal in the OS in the same way as HTTP requests can come from any sender that can access port 8080 on the OS.
I hope I am able to implement an OS-agnostic implementation of this idea in rust. I did try googling this idea without much luck; Likely because I am missing the terminology of this idea.
To clarify one use case:
In one terminal I start my "CLI server": Example: cli_server start
In another terminal I make a "CLI request" to the server and the server will handle the request and respond. The response should be pasted on std.out on the requesting terminal. Example: cli_server fetch_note id-22 or cli_server update_note data_in_file.json
The Server should keep alive until asked to shutdown. It is not known how many independent terminals will access the server at a time.
What I imagine I need is some code similar to the code that listens on port 8080.
It is fine to split the code in a cli_server and cli_client if that makes the answer more simple (or possible).
I do know that I could just implement a web server and use curl to hit port 8080. I am prepared for this as a workaround in case there is no answer to this question. I am interested in an answer to this question out of curiosity and also because that would be a neater solution in my case.
Thanks for reading and I hope my question is not too open ended.

How is it possible to identify a website using the process id and port?

I am trying to link a website to a particular opened port. However, using both netstat and lsof, I found that when I open a single website on firefox (say facebook), there are multiple process and ports opened.I don't understand why a single website open multiple processes and ports? Should it not be that one application associates with one single process and port number?
Could anyone please suggest me, how is it possible to identify a website using the process id and port used ?
As mentioned above, I have tried netstat and lsof, which gives multiple process id and port number which makes it hard to uniquely identify the website.
Thank you
If you are the server providing services, the port is manage by you and the service example http is 80, https is 443.
In the other hand if you are the client, your browser will use an available port, that means that the port you use is not going to be the same and is not going to be 80 or 443.
Websites can have content from many sources, images can be on a server, javascript, css can be on another server and the main content must be from the actual web-site.
Hope it helps.

UDP punch hole for a web-server?

For my academic project, I am trying to achieve this.
A web server node JS application listening on port 3000.
So If you curl http://localhost:3000 you will get Hello World!. ( A simple web page.
Now I am running above webserver in my local machine. And my modem is behind NAT. Suppose If I port forward in the modem to myip:3000 then it is open to the world. But here is the biggest thing I am stuck - I don't want to use the modem for port forwarding, instead, I will use third party server for UDP Punch Hole.
Now my requirement is anyone from net should able to access my webserver at curl http://third-party-server-ip:3000.
What I am trying is to write another client - which opens a connection to the third party server. Say it did a hole punching at port 41234. That port is open. The third-party host can send something to that port.
Now anyone in the internet initiate this command curl http://third-party-ip:3000 to the third party host. So the third party returns the myip:udpPunchHolePort i.e., myip:41234.
anyone will again curl to myip:41234 it will be received by the node js UDP punch app, so it will redirect to localhost:3000. Finally, the anyone will receive the response from localhost:3000.
My two questions -
Is there any better way than the one I proposed here?
Is there any well-known node-js lib for this kind of stuff, I see,
I can use UDP punch hole. Or I am thinking to write a Lib to do this in general - does this sounds like re-inventing the wheel?
Note -
In this academic project, we are trying to learn how to make any local application open to the world without port forwarding in the modem.
We read on skype protocol analysis, that is also our inspiration.
No, that won't work.
HTTP runs over TCP, not UDP. Punching a UDP hole doesn't do you any good -- any TCP connection to the backend HTTP server will still fail.
HTTP redirects are not magic. If a user cannot access a specific host:port, redirecting them to a URL on that host:port will just make their browser time out when it requests that URL.
You cannot send a response from a different host:port from what the browser requested, because there is no TCP connection established with that endpoint.

Hosting Nodejs application without port

I have a nodejs application running on port 3000. I wanted to host it on Linux environment. So I installed nodejs in it. It's working fine but I should specify the port each time.
example: mydomain.net:3000/url_i_want,
How can I avoid this. and also when running my app like that, all users are kind of connected to each others. If one of them disconnect all other users are. If one of them change page all others have there pages changing. Is it because they are all listening to the same port 3000 ? I searched and found that it can be related to PM2 and Nginx. Is it the solution ?
Whenever you load a URL without specifying the port number, the browser defaults to 80, because 80 is the default port number for HTTP.
So if you load http://stackoverflow.com/questions, the browser "converts" it to http://stackoverflow.com:80/questions.
If you don't want a port number to be specified to access your website, your app should be listening on port 80, instead of 3000.
However, it is not recommended for Node apps to directly listen on port 80 (although they very well can).
What you can do is use a front-facing proxy such as nginx, which accepts connections to the host's port 80, and then redirects the request to localhost:3000, where your app is listening.
It is best to ask one question at a time.
As for your second question, unless you are using some sort of "remote syncing" framework, that sort of behavior is unexpected. I would suggest posting a separate question for that issue with more details about it.

significance of node.js port choice

I am a bit new to server side scipts. I am fairly capable with javascript so I decided to take a look at node.js as opposed to php or python ect. Correct me if I am wrong but it seems that when I code my webserver I may freely choose the port number I listen to. Is there any significance to this port number or may I choose any random number I wish? Will anyone be able to send a request to my server regardless of the number I choose?
Thanks in advance!
If you want to run node.js directly without any supporting web server or reverse proxy (no nginx, varnish, apache, etc), you need to listen on port 80 for HTTP and (optionally) 443 for HTTPS if you want normal URLs to work. Otherwise users will need to type the port number in the URL like http://example.com:3000 which is unheard of for public-facing sites.
However, you almost certain DO want to use a separate web server as I describe in detail here, in which case any port over 1024 is fine.
If you have root access you can choose any port that's not already bound to a process.
If you do not have root access you can choose any port above 1024 that is not already bound to a process.
Port 80 is usually the one you want to use if you're serving up HTTP, however, you can access an HTTP server on any port via the URL port syntax. For example, a server on port 3000. http://yourdomain.com:3000
If you're running on Linux and you do not want to run your Node process as root, you can redirect port 80 traffic to another port.

Resources