handler handles Azure Blob Storage API requests
(rw http.ResponseWriter, r *http.Request)
| 52 | |
| 53 | // handler handles Azure Blob Storage API requests |
| 54 | func (s *TestServer) handler(rw http.ResponseWriter, r *http.Request) { |
| 55 | s.mu.Lock() |
| 56 | defer s.mu.Unlock() |
| 57 | |
| 58 | // Parse path: /{container}/{blob} |
| 59 | parts := strings.SplitN(strings.TrimPrefix(r.URL.Path, "/"), "/", 2) |
| 60 | if len(parts) < 2 { |
| 61 | rw.WriteHeader(http.StatusBadRequest) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | container := parts[0] |
| 66 | blobName := parts[1] |
| 67 | key := fmt.Sprintf("%s/%s", container, blobName) |
| 68 | |
| 69 | // Handle different Azure Blob Storage operations |
| 70 | comp := r.URL.Query().Get("comp") |
| 71 | blockID := r.URL.Query().Get("blockid") |
| 72 | |
| 73 | switch { |
| 74 | case r.Method == http.MethodPut && comp == "block" && blockID != "": |
| 75 | // StageBlock operation |
| 76 | data, err := io.ReadAll(r.Body) |
| 77 | if err != nil { |
| 78 | rw.WriteHeader(http.StatusInternalServerError) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | // Initialize block data map if needed |
| 83 | if s.blockData[key] == nil { |
| 84 | s.blockData[key] = make(map[string][]byte) |
| 85 | } |
| 86 | |
| 87 | // Store the block data |
| 88 | s.blockData[key][blockID] = data |
| 89 | |
| 90 | // Track block ID in order |
| 91 | if s.stagedBlocks[key] == nil { |
| 92 | s.stagedBlocks[key] = []string{} |
| 93 | } |
| 94 | |
| 95 | // Only add if not already present |
| 96 | if !slices.Contains(s.stagedBlocks[key], blockID) { |
| 97 | s.stagedBlocks[key] = append(s.stagedBlocks[key], blockID) |
| 98 | } |
| 99 | |
| 100 | rw.WriteHeader(http.StatusCreated) |
| 101 | |
| 102 | case r.Method == http.MethodPut && comp == "blocklist": |
| 103 | // CommitBlockList operation |
| 104 | body, _ := io.ReadAll(r.Body) |
| 105 | |
| 106 | // Parse block IDs from XML (simplified - just extract blockid values) |
| 107 | blockIDs := []string{} |
| 108 | for _, id := range s.stagedBlocks[key] { |
| 109 | if strings.Contains(string(body), id) { |
| 110 | blockIDs = append(blockIDs, id) |
| 111 | } |
no test coverage detected