Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom HTTP method with Nodejs HTTP Server

Currently writing a synchronization mechanism over HTTP, I started to build my server on NodeJS, which looked like a good solution.

So Ok, I can get GET and HEAD methods working. But as soon as I try to use non standard methods, for example "SYNC". Here's the code:

var http = require("http");
var server = http.createServer(function (req, res) {
    res.writeHead(200);
    res.end(req.method);
});
server.listen(8080);

Looks alright... let's telnet this, issue a GET and then a SYNC request

mylaptop:~ aob$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked

3
GET
0

SYNC / HTTP/1.1
Connection closed by foreign host.

Any idea on how I can get SYNC to work ?

like image 649
aspyct Avatar asked Apr 30 '12 09:04

aspyct


1 Answers

Edit: The http parser has since been updated and changed quite a lot. Because of this, it's not quite as straight-forward to add custom HTTP methods, but thanks to @admix - here's the relevant entry point in node 5.x:

    switch (ch) {
      case 'A': parser->method = HTTP_ACL; break;
      case 'B': parser->method = HTTP_BIND; break;
      case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
      case 'D': parser->method = HTTP_DELETE; break;
      case 'G': parser->method = HTTP_GET; break;
      case 'H': parser->method = HTTP_HEAD; break;
      case 'L': parser->method = HTTP_LOCK; /* or LINK */ break;
      case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
      case 'N': parser->method = HTTP_NOTIFY; break;
      case 'O': parser->method = HTTP_OPTIONS; break;
      case 'P': parser->method = HTTP_POST;
        /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
        break;
      case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break;
      case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
      case 'T': parser->method = HTTP_TRACE; break;
      case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
      default:
        SET_ERRNO(HPE_INVALID_METHOD);
        goto error;
    }

Original reply:

You would have to patch node in order to add arbitrary methods. See the relevant source lines:

static inline Persistent<String>
method_to_str(unsigned short m) {
  switch (m) {
    case HTTP_DELETE:     return delete_sym;
    case HTTP_GET:        return get_sym;
    case HTTP_HEAD:       return head_sym;
    case HTTP_POST:       return post_sym;
    case HTTP_PUT:        return put_sym;
    case HTTP_CONNECT:    return connect_sym;
    case HTTP_OPTIONS:    return options_sym;
    case HTTP_TRACE:      return trace_sym;
    case HTTP_PATCH:      return patch_sym;
    case HTTP_COPY:       return copy_sym;
    case HTTP_LOCK:       return lock_sym;
    case HTTP_MKCOL:      return mkcol_sym;
    case HTTP_MOVE:       return move_sym;
    case HTTP_PROPFIND:   return propfind_sym;
    case HTTP_PROPPATCH:  return proppatch_sym;
    case HTTP_UNLOCK:     return unlock_sym;
    case HTTP_REPORT:     return report_sym;
    case HTTP_MKACTIVITY: return mkactivity_sym;
    case HTTP_CHECKOUT:   return checkout_sym;
    case HTTP_MERGE:      return merge_sym;
    case HTTP_MSEARCH:    return msearch_sym;
    case HTTP_NOTIFY:     return notify_sym;
    case HTTP_SUBSCRIBE:  return subscribe_sym;
    case HTTP_UNSUBSCRIBE:return unsubscribe_sym;
    default:              return unknown_method_sym;
  }
}
like image 92
Linus Thiel Avatar answered Oct 05 '22 13:10

Linus Thiel