I am trying to create a simple http server in C++ whose home page is (in HTML):
<!DOCTYPE html>
<html>
<head>
<title>home.html</title>
</head>
<body>
<form name="input" action="login.html" method="get">
user name: <input type="text" name="user"><br>
password: <input type="text" name="password">
<input type="submit" value="Submit">
</form>
</body>
</html>
When the user clicks on the submit button after filling in the user and password field, the following page should appear:
<!DOCTYPE html>
<html>
<head>
<title>Contact info</title>
</head>
<body>
<h1>User: games</h1>
<h1>Password: bond</h1>
</body>
</html>
I am using tcpsocket and the code that handles the connection is:
tcp->listenAndAccept();
tcp->send(url.data(), url.length());
char buffer[1000];
tcp->recv(buffer, 1000);
cout << buffer;
tcp->recv(buffer, 1000);
cout << buffer;
The main code is:
int main() {
HTTPServer server(6060);
server.start();
while(true) {
string msg;
cin >> msg;
if (msg == "x") {
break;
}
}
server.kill();
return 0;
}
And the constructor is:
HTTPServer::HTTPServer(int port) {
url = "HTTP/1.1 302 Found \r\nContent-Type: text/html; charset=utf8 \r\nContent-Length:279\r\n\r\n<!DOCTYPE html><html><head><title>Creating an HTML Element</title></head><body><form name=\"input\" action=\"login.html\" method=\"get\">user name: <input type=\"text\" name=\"user\"><br>password: <input type=\"text\" name=\"password\"><input type=\"submit\" value=\"Submit\"></form></body></html>";
tcp = new TCPSocket(port);
}
Now when I enter 127.0.0.1:port
it shows me the home page and prints the response to the console but when I fill out the fields and click on the submit button nothing happens; the second recv
doesn't receive anything. Why is the second recv
call not receiving anything?
Browsers use "?key=value&key2=value2" to set a specific value on a webpage. To support the form, your server needs to check for the question mark character. A good way of doing this is using a pattern (assuming it is multiline).
^GET /?(.*+)(\?(.*))? HTTP/1.[01]$
This regular expression will have either one or three groups. The first one is the actual request that was made (ignoring the slash preceding it). The second one is just to tell the matcher to either match the whole group once or zero times. The final group is the variables.
GET /index.html?user=MyUserName HTTP/1.1
Upon using the regular expression on the HTTP request above, we would capture the following groups.
Group 1 = "index.html"
Group 2 = "?user=MyUserName"
Group 3 = "user=MyUserName"
When clicking the submit button, your form is set to automatically redirect you to "login.html". Adding "method = "get"" is not required since the default method is GET.
The data obtained from the regular expression matcher needs to be handled by the server directly. If the data was correct, you can make the server send the client a cookie containing a valid session ID and display a logged in webpage, otherwise you can make it slightly alter the current webpage.
Good Luck!
Note: You might want to set the input type of the password field to "password".
Note 2: Naming your form doesn't change anything. But if you would like to change it using CSS replace "name=" with "class=" which would be .input in CSS or replace it with "id=", which would make it #input in your CSS.
Feel free to correct me if I made any mistake.
Edit: Does the URL change in the browser address bar if you hit the submit button?
Edit 2: When it's a success code, use a code between (inclusive) 200 and 299.