Sample CalDav example in a PureBasic

Just starting out? Need help? Post your questions and find answers here.
twilliobox
New User
New User
Posts: 2
Joined: Fri Apr 27, 2018 2:04 am

Sample CalDav example in a PureBasic

Post by twilliobox »

Please post a sample code where you can add the task to the calendar using the CalDav protocol.
I do not even know how to start coding http request.

I tried to translate into PureBasic CalDav client on PHP (https://github.com/wvrzel/simpleCalDAV), but my translated example it's not work.

Please, help me...
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sample CalDav example in a PureBasic

Post by infratec »

Hi,

I did CalDAV and CardDAV in PB, but I'm not allowed to publish it.

But here is a snipet for the login:

Code: Select all

    CalDAV_BuildBase64Auth(*Info)
    
    Post$ = "<?xml version=" + #DQUOTE$ + "1.0" + #DQUOTE$ + " encoding=" + #DQUOTE$ + "utf-8" + #DQUOTE$ + " ?>"
    Post$ + "<D:propfind xmlns:D=" + #DQUOTE$ + "DAV:" + #DQUOTE$ + ">"
    Post$ + "<D:prop>"
    Post$ + "<D:resourcetype/>"
    Post$ + "</D:prop>"
    Post$ + "</D:propfind>"
    
    Header$ = "PROPFIND " + *Info\URI$ + " HTTP/1.1" + #CRLF$
    Header$ + "Content-Type: application/xml; charset=utf-8" + #CRLF$
    Header$ + "Content-Length: " + Str(Len(Post$)) + #CRLF$
    Header$ + "Depth: 0" + #CRLF$
    Header$ + "Host: " + *Info\Server$ + #CRLF$
    If *Info\KeepAlive
      Header$ + "Connection: Keep-Alive" + #CRLF$
    EndIf
    Header$ + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + #CRLF$
    Header$ + "Accept-Encoding: identity" + #CRLF$    
    Header$ + "Authorization: Basic " + *Info\Base64Auth$ + #CRLF$
    Header$ + #CRLF$
Bernd
twilliobox
New User
New User
Posts: 2
Joined: Fri Apr 27, 2018 2:04 am

Re: Sample CalDav example in a PureBasic

Post by twilliobox »

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; SAMPLE CODE - NOT WORK!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Intit Network
InitNetwork()

;Connection URL to Google Calendar
ConnectionID = OpenNetworkConnection("https://www.google.com/calendar/dav/user_name/events/", 80)

If ConnectionID

;Create a Post Message
com$ = "PROPFIND HTTP/1.1" + Chr(13) + Chr(10)
com$ = com$ + "Host: https://www.google.com/calendar/dav/user_name/events/" + Chr(13) + Chr(10)
com$ = com$ + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1) Gecko/20090624 Firefox/3.5" + Chr(13) + Chr(10)
com$ = com$ + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + Chr(13) + Chr(10)
com$ = com$ + "Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3" + Chr(13) + Chr(10)
com$ = com$ + "Accept-Encoding: identity" + Chr(13) + Chr(10)
com$ = com$ + "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7" + Chr(13) + Chr(10)
com$ = com$ + "Keep-Alive: 300" + Chr(13) + Chr(10)
com$ = com$ + "Connection: keep-alive" + Chr(13) + Chr(10)
com$ = com$ + "Authorization: Basic " + Chr(13) + Chr(10)

EndIf

;Send POST data
Send = SendNetworkData(Connect, @com$, Len(com$))

Client = NetworkClientEvent(ConnectionID)

Post$ = "<?xml version=" + #DQUOTE$ + "1.0" + #DQUOTE$ + " encoding=" + #DQUOTE$ + "utf-8" + #DQUOTE$ + " ?>"
Post$ + "<D:propfind xmlns:D=" + #DQUOTE$ + "DAV:" + #DQUOTE$ + ">"
Post$ + "<D:prop>"
Post$ + "<D:resourcetype/>"
Post$ + "</D:prop>"
Post$ + "</D:propfind>"

ReceiveNetworkData(ConnectionID,@Post$,Len(Post$)
CloseNetworkConnection(ConnectionID)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Where in the request data need to place login and password?
May need to connect cURL in this example?
Waiting for a post answer and sorry for my english
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sample CalDav example in a PureBasic

Post by infratec »

Hi,

excuse me ...
but your code has so many faults, that I don't know if you can manage CalDAV.
You don't know how to build a html header,
you don't know the PB network stuff,
you don't know ...

Here is an untested code which can work:

Code: Select all

;Intit Network
InitNetwork()

;Connection URL to Google Calendar
ConnectionID = OpenNetworkConnection("www.google.com", 80)

If ConnectionID
  
  *Auth = UTF8("User:Password")
  Auth$ = Base64Encoder(*Auth, MemorySize(*Auth))
  FreeMemory(*Auth)
  
  ;create post data
  ;create post data
  Post$ = "<?xml version=" + #DQUOTE$ + "1.0" + #DQUOTE$ + " encoding=" + #DQUOTE$ + "utf-8" + #DQUOTE$ + " ?>" + #CRLF$
  Post$ + "<D:propfind xmlns:D=" + #DQUOTE$ + "DAV:" + #DQUOTE$ + ">" + #CRLF$
  Post$ + " <D:prop>" + #CRLF$
  Post$ + "   <D:resourcetype/>" + #CRLF$
  Post$ + " </D:prop>" + #CRLF$
  Post$ + "</D:propfind>" + #CRLF$
  
  ;Create header data
  Header$ = "PROPFIND /calendar/dav/user_name/events/ HTTP/1.1" + #CRLF$
  Header$ + "Host: www.google.com" + #CRLF$
  Header$ + "Content-Type: application/xml; charset=utf-8" + #CRLF$
  Header$ + "Content-Length: " + Str(StringByteLength(Post$, #PB_UTF8)) + #CRLF$
  Header$ + "Depth: 0" + #CRLF$
  Header$ + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + #CRLF$
  Header$ + "Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3" + #CRLF$
  Header$ + "Accept-Encoding: identity" + #CRLF$
  Header$ + "Authorization: Basic " + Auth$ + #CRLF$
  Header$ + #CRLF$
  
  ;Send POST data
  If SendNetworkString(ConnectionID, Header$ + Post$, #PB_UTF8)
    
    Timeout = 300
    
    Repeat
      Select NetworkClientEvent(ConnectionID)
        Case #PB_NetworkEvent_Data
          *Buffer = AllocateMemory(2048, #PB_Memory_NoClear)
          If *Buffer
            Length = ReceiveNetworkData(ConnectionID, *Buffer, MemorySize(*Buffer))
            If Length
              Debug PeekS(*Buffer, Length, #PB_UTF8)
            EndIf
            FreeMemory(*Buffer)
          EndIf
          Break
          
        Case #PB_NetworkEvent_None
          Timeout - 1
          Delay(10)
          
        Case #PB_NetworkEvent_Disconnect
          Timeout = 0
          
      EndSelect
    Until Timeout = 0
  EndIf
  
  CloseNetworkConnection(ConnectionID)
  
EndIf
Bernd
Last edited by infratec on Fri Apr 27, 2018 8:49 pm, edited 6 times in total.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sample CalDav example in a PureBasic

Post by infratec »

I made a few changes above.
PROPFIND and Host was not correct.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sample CalDav example in a PureBasic

Post by infratec »

You also use https, but you open Port 80 :!:
Another thing which is not possible.
https uses port 443

But I think OpenNetworkConnection() can not handle https.
You have to use libcurl.pbi if you need https.
Post Reply