logging in to server using Socket Programming - sockets

I am trying to write a code that connect our local server, log in to a webpage on that and retrieve some data. I could connect the server using the server IP and connect function. Now I need to log in on a webpage that accept the following format:
addUPI?function=login&user=user-name&passwd=user-password&host-id=xxxx&mode=t/z
I wrote something like this:
int ret= send(sock,"addUPI?funcion...&mode=t",strlen("addUPI?funcion...&mode=t"),0);
but it does not work. Can anybody help me please?

This isn't really the right way to do HTTP. For one thing, the typical HTTP lifecycle looks something like this (very abbreviated):
...Connect
>>> GET / HTTP/1.0
>>> Host: localhost
>>> Referrer: http://www.google.com
>>>
<<< HTTP/1.0 200 OK
<<< Date: Wed, 08 Apr 2015 05:21:32 GMT
<<< Content-Type: text/html
<<< Content-Length: 20
<<< Set-Cookie: ...
<<<
<<< <html><h1>Hello World</h1></html>
And that's assuming there are no redirects, SSL or other mystical protocol happenings. So, just writing the string you specified above is going to result in a closed connection due to not following the protocol.
Really, you probably want to use a fully-baked HTTP library like cURL, which manages all the protocol requirements.
I shamelessly adapted this example from the curl website:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/addUPI?function=login&user=user-name&passwd=user-password&host-id=xxxx&mode=t");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

Related

Does jetty overwrite custom http status code ?

I am trying to develop a rest api for my service wherein I set custom http status code depending on authorisation failure. But when I test it out using curl I am receiving 404 instead of 403. I am perplexed as to what might be causing this? Please help.
This is what I see from curl output or swagger UI:
root#ubuntu:~# curl -X GET http://localhost:8082/mr/v1/topic/bhakk -v
Note: Unnecessary use of -X or --request, GET is already inferred.
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8082 (#0)
> GET /mr/v1/topic/bhakk HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Mon, 07 May 2018 22:00:10 GMT
< Exception: serviceBlockedException
< Content-Type: application/vnd.kafka.v1+json
< Content-Length: 83
< Server: Jetty(9.2.z-SNAPSHOT)
<
* Connection #0 to host localhost left intact
{"error_code":40301,"message":"This service does not have access to the resource."}
Here is the code:
public Collection<String> list(#HeaderParam("x-nssvc-serviceid") String serviceID) {
Date now = new java.util.Date();
if (! ctx.getSecurityRestrictions().isServiceAllowed(uri, httpHeaders, "Describe", "Cluster", "kafka-cluster"))
throw Errors.serviceBlockedException(ctx,httpServletResponse);
List<String> topicsCopy = new ArrayList<String>(topics);
for (Iterator<String> iterator = topicsCopy.iterator(); iterator.hasNext();) {
String topic = iterator.next();
if (! ctx.getSecurityRestrictions().hasAccess (serviceId, "Describe", "Topic", topic)) {
iterator.remove();
}
}
return topicsCopy;
}
public static RestException serviceBlockedException(Context ctx,HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Exception","serviceBlockedException");
httpServletResponse.setStatus(Status.FORBIDDEN.getStatusCode()); <----// here i am setting status code.
return new RestNotFoundException(SERVICE_ID_BLOCKED_MESSAGE, SERVICE_ID_BLOCKED_ERROR_CODE);
}
Kafka sets the Response 404 status in its RestNotFoundException
See: https://github.com/confluentinc/rest-utils/blob/master/core/src/main/java/io/confluent/rest/exceptions/RestNotFoundException.java

Docker API returns 200 OK then 400 BAD REQUEST

I am writing an API client for Docker. I understood from the documentation that the API is Restful/HTTP, yet if you connect to the local daemon you have to do it over the exposed unix socket.
It all seems to work, I open a socket, send an HTTP request (which respects the specification), I receive the expected response, but also a 400 BAD REQUEST response follows immediately.
Here is the request:
GET /info HTTP/1.1
Host: localhost
Accept: application/json
And here is what I get:
HTTP/1.1 200 OK
Api-Version: 1.30
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/17.06.1-ce (linux)
Date: Thu, 01 Feb 2018 18:53:18 GMT
Transfer-Encoding: chunked
892
{"ID":"6MGE:35TO:BI..." ...}
0
HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close
400 Bad Request
First, I figured that there is a bug on my side and I am somehow sending 2 requests, but I enabled debugging and followed the logs with sudo journalctl -fu docker.service and there is exactly one request received... at least one is logged, the GET /info. I've also debugged the code and 1 single request is sent.
Any hint is greatly appreciated!
Edit: here is the client's code:
final StringBuilder hdrs = new StringBuilder();
for(final Map.Entry<String, String> header : headers) {
hdrs.append(header.getKey() + ": " + header.getValue())
.append("\r\n");
}
final String request = String.format(
this.template(), method, home, hdrs, this.readContent(content)
);
final UnixSocketChannel channel = UnixSocketChannel.open(
new UnixSocketAddress(this.path)
);
final PrintWriter writer = new PrintWriter(
Channels.newOutputStream(channel)
);
writer.print(request);
writer.flush();
final InputStreamReader reader = new InputStreamReader(
Channels.newInputStream(channel)
);
CharBuffer result = CharBuffer.allocate(1024);
reader.read(result);
result.flip();
System.out.println("read from server: " + result.toString());
It seems like you have an extra CRLF between headers and body.
private String template() {
final StringBuilder message = new StringBuilder();
message
.append("%s %s HTTP/1.1\r\n")
.append("Host: localhost").append("\r\n")
.append("%s")
.append("\r\n").append("\r\n") //one of these is superfluous, as each header line ends with "\r\n" itself
.append("%s");
return message.toString();
}
Remove one append("\r\n") after headers and see what happens.
Fixed. Initially, I thought the problem was with the line endings (that they should have been \n instead of \r\n). Turns out, the 400 BAD REQUEST occured because the Connection: close header was missing, while the Request made was being closed right after receiving the response.
More details here.

redirect to a website in c

I am using the following code to redirect client request. But when doing the following the clients are not redirected. Its showing "Unable to connect" in the browser. I redirect the clients to port 8080 using iptables. And running the following executable to redirect. How to redirect the clients. Please provide solution....
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char *reply = "HTTP/1.1 301 Moved Permanently\nServer: Apache/2.2.3\nLocation:
http://www.google.com\nContent-Length: 1000\nConnection: close\nContent-Type:
text/html; charset=UTF-8";
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(8080);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
printf("client connected\n");
send(connfd, reply, strlen(reply), 0);
close(connfd);
sleep(1);
}
}
Please refer to the example in this page or this page to construct a valid http response on the server side. Then, add your html body below it.
Minimum you would need is
HTTP/1.1 200 OK
Content-Length: XXXXX <- put size of the your html body
Connection: close
Content-Type: text/html; charset=UTF-8
I am unable to reproduce the error that you see. You should provide more details (e.g., what kind of client, exact text of iptables rule). For my test, I did not set any iptables rule, and instead pointed the Firefox 12.0 browser directly to localhost:8080.
Splitting up your reply so that it is easier to read shows:
char *reply =
"HTTP/1.1 301 Moved Permanently\n"
"Server: Apache/2.2.3\n"
"Location: http://www.google.com\n"
"Content-Length: 1000\n"
"Connection: close\n"
"Content-Type: text/html; charset=UTF-8"
;
Although the RFC specifies \r\n for line terminators, most clients will accept \n (you don't say which client you are using). But, three other glaring issues are that the last line is not terminated, the response itself is not terminated by a blank line, and you have a Content-Length header of 1000, but no content. Any of these issues could be cause for a client to treat the response as invalid and ignore it.
char *reply =
"HTTP/1.1 301 Moved Permanently\r\n"
"Server: Apache/2.2.3\r\n"
"Location: http://www.google.com\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n"
;
Reading farther into your code, you close the connection immediately after sending your reply without first reading the request. This might lead to an (albeit unlikely) race where you close the connection before the request is fully delivered to the server. Then, when the request does arrive, it will trigger a reset to the client, and the response could be dropped. So, you should add code to make the delivery of your reply more robust:
printf("client connected\n");
send(connfd, reply, strlen(reply), 0);
shutdown(connfd, SHUT_WR);
while (recv(connfd, sendBuff, sizeof(sendBuff), 0) > 0) {}
close(connfd);
Given that I cannot reproduce your issue with the response as it is, though, it is also possible that you did not set your iptable redirect rule properly.

Luasocket custom headers, 404 turns to 301

My previous question was about fetching page title in lua using the socket.http module. The question lies here. Previously, youtube pages led me to a 404 error page. Based on MattJ's help, I put up custom HOST header for the request. This is what I did and what was the result:
Code
header = { host= "youtube.com" }
local result,b,c,h = http.request{ url = "http://www.youtube.com/watch?v=_eT40eV7OiI", headers = header }
print ( result, b, c, h )
for k,v in pairs(c) do print(k,v) end
Result
1 301 table: 0047D430 HTTP/1.1 301 Moved Permanently
x-content-type-options nosniff
content-length 0
expires Tue, 27 Apr 1971 19:44:06 EST
cache-control no-cache
connection close
location http://www.youtube.com/watch?v=_eT40eV7OiI
content-type text/html; charset=utf-8
date Sat, 28 Apr 2012 04:26:21 GMT
server wiseguy/0.6.11
As far as I was able to understand from this, the error is basically because of X-Content-Type-Options valued nosniff. Reading its documentation, I got to know that the only defined value, "nosniff", prevents Internet Explorer from MIME-sniffing a response away from the declared content-type.
Please help me so that I can use custom proxy and fetch the youtube(and some other sites, as mentioned in the previous question) title from their body. Here is the complete LUA file I currently have:
local http = require "socket.http"
http.PROXY="http://<proxy address here>:8080"
header = { host= "youtube.com" }
local result,b,c,h = http.request{ url = "http://www.youtube.com/watch?v=_eT40eV7OiI", headers = header }
print ( result, b, c, h )
for k,v in pairs(c) do print(k,v) end
I believe this line should be changed:
header = { host= "youtube.com" }
To:
header = { host= "www.youtube.com" }
After that, works for me.
The solution is to install luasec and to use ssl.https module to do the request.
Answered here by Paul Kulchenko!
Example:
-- luasec version 0.4.2
require("ssl")
require("https")
-- ssl.https.request(...)

SOAP servers and clients with Zend framework (Getting errors)

I am testing a server and client i made on my webspace.
when i try to call a simple "testServer" function defined in a ServerMap class, I get
"Looks like we got no XML document"
..?
I called getFunctions on the client and testServer is a valid function. I tried catching all exceptions and then calling __getLastResponseHeaders() and __getLastResponse.
header:
string(348) "HTTP/1.1 200 OK
Date: Tue, 23 Jun 2009 19:36:29 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
X-Powered-By: PHP/5.2.9
Cache-Control: max-age=1
Expires: Tue, 23 Jun 2009 19:36:30 GMT
Vary: Accept-Encoding
Content-Length: 1574
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
"
response:
string(1574) "DEBUG HEADER : This is a cached page !
"
If i look at the source html of the response, its actually:
string(1574) "DEBUG HEADER : This is a cached page !<?xml version="1.0"?>
<A lot of xml that looks pretty much like my WSDL file that my Zend_Soap_AutoDiscover generates>
So whats going on? I searched online and i didnt really find any solid solutions.
I don't have blank space before my ..
If you are outputting to the browser, it is hiding the xml cause it is in a . Browsers ignore tags they don't understad.
Do a echo htmlentities($output); to see the xml tags.
Not sure what your problem is, but I can provide a bit of code that I know works for us using Zend Framework 1.8x as a backend SOAP service for silverlight and WCF. This service simples takes 2 integers, adds them and returns the result. Simple as you can get.
Controller Class example:
class SoapController extends Zend_Controller_Action {
/*
* SOAP server action
*/
public function indexAction() {
$request = $this->getRequest();
if ($request->getParam('wsdl') !== null) {
$wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setClass('SoapMath');
$wsdl->handle();
}
else {
$module = $request->getModuleName();
$controller = $request->getControllerName();
$uri = 'http://' . Zend_Registry::get('fullUrl') . '/' . $module . '/' . $controller . '?wsdl';
$server = new Zend_Soap_Server($uri);
$server->setClass('SoapMath');
$server->handle();
}
exit;
}
}
And the actual work is done by 'SoapMath' which is defined as:
class SoapMath {
public function add($a,$b) {
return ($a + $b);
}
}