int http_download_photo(char *path, char *email) { struct connection *conn; struct connect_options conn_opts = {0}; int error_code; int rc; if (!open_socket_lib()) return 0; rc = 0; if ((conn = tcp_connect("simplemail.sourceforge.net", 80, &conn_opts, &error_code))) { char *line; int download = 0; tcp_write(conn,"GET /gallery_get_image.php?",sizeof("GET /gallery_get_image.php?")-1); tcp_write(conn,email,strlen(email)); tcp_write(conn," HTTP/1.0\r\nhost: simplemail.sourceforge.net\r\n\r\n",sizeof(" HTTP/1.0\r\nhost: simplemail.sourceforge.net\r\n\r\n")-1); while ((line = tcp_readln(conn))) { if (!mystrnicmp("Content-Type: image/",line,20)) download = 1; if (line[0] == 10 && line[1] == 0) break; } if (download) { FILE *fp = fopen(path,"wb"); if (fp) { int got; char buf[1024]; while ((got = tcp_read(conn,buf,1024))>0) { fwrite(buf,1,got,fp); } rc = 1; fclose(fp); } } tcp_disconnect(conn); } close_socket_lib(); return rc; }
int open_ssl_lib(void) { #ifdef NO_SSL return 0; #else struct thread_s *thread; SM_ENTER; if (!(thread = (struct thread_s*)FindTask(NULL)->tc_UserData)) goto out; if (!open_socket_lib()) goto out; #ifndef USE_OPENSSL if (!thread->amissllib) { #ifdef USE_AMISSL3 if (open_amissl3(thread)) { #else SM_DEBUGF(10,("Open amissl.library\n")); if ((thread->amissllib = OpenLibraryInterface("amissl.library",1,&thread->iamissl))) { if (!InitAmiSSL(AmiSSL_Version, AmiSSL_CurrentVersion, AmiSSL_Revision, AmiSSL_CurrentRevision, AmiSSL_SocketBase, (ULONG)SocketBase, /* AmiSSL_VersionOverride, TRUE,*/ /* If you insist */ TAG_DONE)) { #endif #endif if ((thread->ssl_ctx = ssl_create_context())) { /* Everything is ok */ thread->ssllib_opencnt = 1; SM_DEBUGF(10,("AmiSSL opened %ld times\n",thread->ssllib_opencnt)); SM_RETURN(1,"%ld"); return 1; } #ifndef USE_OPENSSL #ifdef USE_AMISSL3 close_amissl3(thread); } #else CleanupAmiSSL(TAG_DONE); } CloseLibraryInterface(thread->amissllib,thread->iamissl); thread->iamissl = NULL; thread->amissllib = NULL; } #endif } else { thread->ssllib_opencnt++; SM_DEBUGF(10,("AmiSSL opened %ld times\n",thread->ssllib_opencnt)); SM_RETURN(1,"%ld"); return 1; } #else /* USE_OPENSSL */ return 1; #endif out: close_socket_lib(); SM_RETURN(0,"%ld"); return 0; #endif }
int http_download(char *uri, void **buf_ptr, int *buf_len_ptr) { int rc = 0; if (!mystrnicmp(uri,"http://",7)) { int port; char *path_buf; char *port_buf; struct connection *conn; struct connect_options connect_opts = {0}; int error_code; char *server; uri += 7; if (!(server = mystrdup(uri))) return 0; if (!(path_buf = strchr(server,'/'))) { free(server); return 0; } port_buf = strchr(uri,':'); if (port_buf > path_buf) port_buf = NULL; if (port_buf) { *port_buf = 0; port = atoi(port_buf+1); } else { *path_buf = 0; port = 80; } if (open_socket_lib()) { if ((conn = tcp_connect(server,port,&connect_opts,&error_code))) { FILE *fh; if ((fh = tmpfile())) { int download = 1; char *line; tcp_write(conn,"GET /", sizeof("GET /")-1); tcp_write(conn,path_buf+1,strlen(path_buf+1)); tcp_write(conn," HTTP/1.0\r\nhost: ", sizeof(" HTTP/1.0\r\nhost: ")-1); tcp_write(conn,server,strlen(server)); tcp_write(conn,"\r\n\r\n",sizeof("\r\n\r\n")-1); while ((line = tcp_readln(conn))) { if (line[0] == 10 && line[1] == 0) break; } if (download) { int got; int len = 0; char buf[1024]; while ((got = tcp_read(conn,buf,1024))>0) { fwrite(buf,1,got,fh); len += got; } if ((*buf_ptr = malloc(len))) { fseek(fh,0,SEEK_SET); fread(*buf_ptr,1,len,fh); *buf_len_ptr = len; rc = 1; } } fclose(fh); } tcp_disconnect(conn); } close_socket_lib(); } free(server); } return rc; }
/** * Open the socket library, but make sure that this happns only once. * * @return socket is open (1) or not (0). */ static int imap_open_socket_lib(void) { if (!imap_socket_lib_open) imap_socket_lib_open = open_socket_lib(); return imap_socket_lib_open; }