1. Paweł Wieczorek
  2. ocaml-sip

Commits

Paweł Wieczorek  committed b2add5e Draft

sip request as object

  • Participants
  • Parent commits 5e5d4e8
  • Branches default

Comments (0)

Files changed (1)

File src/Sip_types.ml

View file
     | PRACK
     | BYE
     | OPTIONS
+    | SUBSCRIBE
+    | NOTIFY
+    | PUBLISH
+    | INFO
+    | REFER
+    | MESSAGE
+    | UPDATE
     | UnknownRequestMethod of string
 
 let sip_request_method_of_string = function
 type sip_cseq =
     SipCseq of string
 
-type sip_
+type sip_contact =
+    SipContact of string
 
-type sip_request =
-    { sip_request_method : sip_request_method
-    ; sip_uri            : sip_uri
-    ; sip_version        : string
-    ; sip_mime           : (string, string) Hashtbl.t
-    ; sip_content_length : int
-    ; sip_content        : string
-    }
+type sip_version =
+    | SIP_1_0
+    | SIP_2_0
+    | UnknownSipVersion of string
+
+let sip_version_of_string = function
+    | "SIP/1.0" -> SIP_1_0
+    | "SIP/2.0" -> SIP_2_0
+    | sipver    -> UnknownSipVersion sipver
+
+class sip_request _reqmethod _uri _version mime =
+
+        let reqmethod      = sip_request_method_of_string _reqmethod in
+
+        let content_length = int_of_string (Hashtbl.find mime "content-length") in
+
+        let addr_from      = SipUri (Hashtbl.find mime "from") in
+
+        let addr_to        = SipUri (Hashtbl.find mime "to") in
+
+        let call_id        = SipCallId (Hashtbl.find mime "call-id") in
+
+        let cseq           = SipCseq (Hashtbl.find mime "cseq") in
+
+        let vias           = Hashtbl.find_all mime "via" in
+
+        let version        = sip_version_of_string _version in
+
+        let uri            = SipUri _uri in
+
+        let sip_contact    = SipContact (Hashtbl.find mime "contact") in
+
+        let max_forwards   = int_of_string (Hashtbl.find mime "max-forwards") in
+
+    object
+        val mime                = mime
+
+        method sip_method       = reqmethod
+
+        method sip_uri          = uri
+
+        method sip_version      = version
+
+        method content_length   = content_length
+
+        method addr_to          = addr_to
+
+        method addr_from        = addr_from
+
+        method vias             = vias
+
+        method call_id          = call_id
+
+        method cseq             = cseq
+
+        method max_forwards     = max_forwards
+
+        method contact          = sip_contact
+
+        method header name      = Hashtbl.find mime name
+
+        method headers name     = Hashtbl.find_all mime name
+    end
+
 
 (***************************************************************************************************)
 (* PARSER, unsafe *)
     (sip_request_method_of_string strmethod, last)
 
 let parse_request_line buffer =
-    let (reqmethod, last) = parse_request_method buffer in
+    let (reqmethod, last) = find_word 0    buffer in
     let (uri,       last) = find_word last buffer in
     let (ver,       last) = find_word last buffer in
     let (_,         last) = find_endline last buffer in
             (htable, last + 2)
         else
             let (name, value, last) = parse_entry last in
-            Hashtbl.add htable name value;
+            Hashtbl.add htable (String.lowercase name) value;
             parse_lines last
         in
 
     parse_lines
 
-let parse_request buffer =
+let early_parse_request buffer =
     try 
         let (reqmethod, uri, sipver, last) = parse_request_line buffer in
         let (mime, last)                   = parse_mime buffer last in
-        { sip_request_method = reqmethod
-        ; sip_uri            = SipUri uri
-        ; sip_version        = sipver
-        ; sip_mime           = mime
-        ; sip_content        = String.slice buffer ~first:last 
-        ; sip_content_length = String.length buffer - last
-        }
+        (reqmethod, uri, sipver, mime, last)
     with
         Not_found | Invalid_argument _ ->
             raise Invalid_sip_message
 
+let parse_request buffer =
+    let (reqmethod, uri, sipver, mime, last) = early_parse_request buffer in
+    try
+        new sip_request reqmethod uri sipver mime
+    with
+        Not_found | Failure _ ->
+            raise Invalid_sip_message
+
 let test1 = 
     "INVITE sip2 SIP/2.0\r\n" ^
     "A: 20\r\n" ^