onlyforbopi
4/16/2018 - 7:12 PM

http theory

  1. theory (to sort)
URL

http://www.abc.org:80/bla/bla2/bla3/something.html

http:// = url scheme (can be https, ftp, etc, mailto:)
mailto:skpatel@gmail.com (mailto url - other type)

www.abc.org = host
host can be ip address
Host is used to uniquely identify the part of the webserver where the resource is located

:80 = Port number (Not always specified, if not followes default for scheme ie 80 for http)
On serer side, every server application will be listening on specific port numbers (ie 80, 443, 25)
From client side, browsers will try to connect to specific port of the available.

standard analogies 80 = http
                   443 = https 
	* in case port is missing
	
/bla/bla2/bla3 = Url path ( in dir structure )

something.html = resource 
static resource = doesnt change
dynamic resource = does change
web client sends request to web application / server who finds equivalent resource and sends it to the client

# 
URL WITH QUERY STRING
(Get request - see below)
http://www.abc.org:80/bla/bla2/bla3/?ID=31&STATUS=2


URL WITH QUERY AND FRAGMENT
http://www.abc.org:80/bla/bla2/?ID=10#/discussions
Used to return only a portion of the resource. 
fragment is not passed to webserver
fragment is like a mini-bookmark



URL ENCODING 
Specific characters are not allowed in URL
ie space -> in URL will be %20
http://www.abc.com/my%20file.html

EACH URL POINTS TO A UNIQUE RESOURCE ON THE WEBSERVER



WEB CLIENT -> REQUEST -> WEBSERVER (CONTAINED IN URL, OR POST/PUT/UPDATE)
WEBSERVER -> RESPONSE -> WEBCLIENT (DATA CONTAINED IN BODY OF RESPONSE)


STATELESS
WHEN THE REQUEST - RESPONSE HAPPENS THE CONNECTIONS IS CLOSED AND THE 
WEB SERVER DOES NOT REMEMBER ANYTHING ELSE ABOUT THE REQUEST OR HISTORY.
IF MORE REQUESTS HAPPEN THE SAME WILL HAPPEN AGAIN, WITH NO MEMORY.
THAT'S WHY HTTP IS CALLED A STATELESS PROTOCOL.







Http Get vs Post

FIDDLER TO TEST


Main issue : How to transmit data between browser - server 
in a way that they can both understand it, regardless of whats runnign on them.
Webserver is waiting on request that client(browser) sends.
Webserver hosts Web Application which listens to specific ports
Web Clients (Or Browsers) will send requests to webserver, and receive responses (Browsers, curl, telnet etc)

HTTP REQUEST / RESPONSE SYSTEM


Http Request
This is the request from the browser, to the webserver.

Method:
GET / POST / PUT / DELETE
READ / CREATE / UPDATE / DELETE ENTITY IN DB

Request Header:
Contains additional information about the request
What type of response is required from the server (xml, json, or other format)

Request body:
Data we want to send to the server
ie a Post request would contain the data of the new row we want to create (data can be in xml, json, etc)


Http Response
This is the response from the webserver, to the browser.

Response Header / Status Code
Gives details of the status of request
200 - ok
204 - no content
404 - not found

Response Body
In request header we specify what type of response (xml, json, etc is required)
In response body we will have the data of the response, formatted in the requested format (xml, json, etc)

Two main methods :

#####
Get
Here the string of parameters is passed in the URL

GET HAS NO REQUEST BODY

ie www.abc.com?ID=123&name=bob
The string ID... is passed from browser to webserver, passing some parameters
Webserver sees browser wants to communicate with www.abc.com
webserver sees no filename is passed, so he ll access www.abc.com/index.php
Webserver will pass parameters into index.php

* if url was www.abc.com/Files?ID=... it would look into the www.abc.com/Files
* www.abc.com is the space the domain has on the webserver

? = stards parameter string
& = separate parameters
ID=123&name=BOB => passed parameters ID=123, and name=BOB

ie get with script
www.abc.com/script.php?parameters...

ie get in html (method isnt necessary)
* get method will append value pairs to the URL
<form method="get" action = "http://www.abc.com/script.php>

GET METHOD PASSES VALUEPAIRS TO URL
THERE ARE LIMITATIONS ON AMOUNT OF DATA SENT TO THE URL LINE. (1024/2048)
USED FOR FETCHING DOCUMENTS
GET REQUESTS SHOULD NOT CHANGE STATE OF SERVER
GET REQUESTS SHOULD BE ABLE TO HAPPEN OVER AND OVER WITHOUT SERVER CHANGE = SAME RESULT


* Large variable values : 2000 characters
* Visible to everyone
* Should not be used when sending passwords
* Only ascii allowed
* Can be bookmarked 
* Remains in browser history / can be cached
* Doesnt alert before re-execution

C# / ASP.NET

# Some data declaration, static, or db
static List<string> strings = new List<string>()
{
	"value0", "value1", "value2"
}

# Static Get
# Get always RETURNS something (View/String/Something)
public IEnumerable<string> Get()
{
	return strings;
}

# Return one record
# Overloaded get - with parameter - ie fetch specific row (or rows)
public string Get(int id)
{
	return strings[id]
}


#####
Post
Everything is same with get, but the variables are sent down with the actual post data
instead of as a part of the URL. 
The variables will be contained in the Post request.

<form method="post" action = "http://www.abc.com/script.php>


Post method DOES NOT append value pairs to URL
Post method is sending it with a separate header file?
It is still not secure, it is contained in Post Header
POST REQUESTS SHOULD CHANGE STATE OF SERVER
POST REQUESTS CAN BE DESTRUCTIVE IN NATURE.

PARAMETERS IN BODY OF POST
USED FOR UPDATING DATA

* Large variable values : 8mb limit
* Not displayed in URL
* Should be used when sending passwords
* Binary data allower
* Cannot be bookmarked (the response)
* Doesnt remain in browser history / cache
* Alerts before re-execution (resubmit data?)

# Post method does not have to return something
# Post method can redirect to view (not return)
# Post method is used to Change Data on the server
public void Post([FromBody] string value)
{
	strings.Add(Value)
}


#####
GET / POST ANALOGY WITH CRUD OPERATIONS IN DB

CREATE ROW = POST (CHANGES DB)
READ ROWS = GET (DOES NOT CHANGE DB)
UPDATE = PUT (CHANGES DB - CAN BE DONE WITH GET(ROW DATA) - CHANGE(EDIT) - POST(UPDATE))
DELETE = DELETE (CHANGES DB - CAN BE DONE WITH POST TOO)

######

PUT METHOD (Used to Update values)

# Update element in id index.
# Id and value are passed as parameters
public void Put(int id, [FromBody] string value)
{
	strings[id] = value;
}

######

DELETE METHOD (Used to delete elements)

public void Delete(int id)
{
	strings.removeAt(id)
}


############################################

HTTP HEADERS + POPULAR USES